Upload images png to the server [closed]

0

I am developing an application with html5 , css3 , jquery and php , and I have the problem that when I want to upload a png image, it does not upload it to the server. I can upload images jpg, bmp, but when the image is png it seems that it does not detect it. Any idea why this happens?

This is the code that you use: the temporary file if you create it but do not upload it, it seems that the problem is in the mov_upload statement. The strange thing is that if it works with other types of images. the problem is just "png"

//revisa si viene imagen
if ($_FILES["archivo"]["name"]!=""){
    echo $_FILES["archivo"]["type"];

    //verifica tipo de imagen, capturando la extension del nombre del archivo
    //vrifica la posocion del punto (.), para los archivos jpeg
    $ext=substr($_FILES["archivo"]["name"], -4);
    if (substr($_FILES["archivo"]["name"], -4, 1)!="."){
        $ext=substr($_FILES["archivo"]["name"], -5);
    }
    $ext=substr($ext, 1); //quita el punto de la extension

    if($ext=="jpeg" or $ext=="jpg" or $ext=="bmp"){
        $imagen=time() . ".$ext";

        $destino="media.image/publicacion/" . $imagen;
        $archivo=$_FILES["archivo"]["tmp_name"];

        move_uploaded_file($archivo, $destino);
    }
}
    
asked by Filino 15.08.2017 в 22:19
source

1 answer

3

As indicated in the comments, your problem is to add one more condition to your if .

//revisa si viene imagen
if ($_FILES["archivo"]["name"]!=""){
    echo $_FILES["archivo"]["type"];

    //verifica tipo de imagen, capturando la extension del nombre del archivo
    //vrifica la posocion del punto (.), para los archivos jpeg
    $ext=substr($_FILES["archivo"]["name"], -4);
    if (substr($_FILES["archivo"]["name"], -4, 1)!="."){
        $ext=substr($_FILES["archivo"]["name"], -5);
    }
    $ext=substr($ext, 1); //quita el punto de la extension

    if($ext=="jpeg" or $ext=="jpg" or $ext=="bmp" or $ext=="png"){
        $imagen=time() . ".$ext";

        $destino="media.image/publicacion/" . $imagen;
        $archivo=$_FILES["archivo"]["tmp_name"];

        move_uploaded_file($archivo, $destino);
    }
}

Another way to validate and not complicate, is to use an Array in which you will have all your extensions "valid" and using the function in_array valid that is contained in these extensions.

For example:

//revisa si viene imagen
if ($_FILES["archivo"]["name"]!=""){
    echo $_FILES["archivo"]["type"];

    //verifica tipo de imagen, capturando la extension del nombre del archivo
    //vrifica la posocion del punto (.), para los archivos jpeg
    $ext=substr($_FILES["archivo"]["name"], -4);
    if (substr($_FILES["archivo"]["name"], -4, 1)!="."){
        $ext=substr($_FILES["archivo"]["name"], -5);
    }

    $ext = substr($ext, 1); //quita el punto de la extension

    $extensionesValidas = array("jpeg", "jpg", "bmp", "png");

    if(in_array($ext, $extensionesValidas)){
        $imagen=time() . ".$ext";

        $destino="media.image/publicacion/" . $imagen;
        $archivo=$_FILES["archivo"]["tmp_name"];

        move_uploaded_file($archivo, $destino);
    }
}

Likewise, you can take advantage of the accept attribute in your input file.

<input type="file" name="archivo" accept="image/*">

There it indicates that it will allow to visualize and select the image type files.

If you want to specify the types:

<input type="file" name="archivo" accept=".jpg,.jpeg,.bmp,.png">

Reference:

answered by 15.08.2017 в 23:20