Error with getimagesize in photo upload

Asked

Viewed 254 times

0

I’m trying to set up a system that checks the mime type image when uploading. However.

Follows the code:

<?php 

$action = addslashes(filter_input(INPUT_GET, 'action',FILTER_SANITIZE_SPECIAL_CHARS));

if ((!empty($action)) and ($action == "add")) {
   // Recebe a imagem 
   $imagem = $_FILES["imagem"];

   $size = getimagesize($imagem); 

   switch ($size['mime']) { 
       case "image/gif": 
           echo "Image is a gif"; 
           break; 
       case "image/jpeg": 
           echo "Image is a jpeg"; 
           break; 
       case "image/png": 
           echo "Image is a png"; 
           break; 
       case "image/bmp": 
           echo "Image is a bmp"; 
           break; 
   } 
}

?>

<form name='form' method='post' action='?action=add' enctype='multipart/form-data'>
    <input type="file" name="imagem">
    <input type="submit" value="ok">
</form>

Error log:

Warning: getimagesize() expects Parameter 1 to be string, array Given in /Library/Webserver/Documents/teste.php on line 12

Error occurs on this line:

$size = getimagesize($imagem); 

I modified it to

$image = $_FILES['image']['tmp_name'];

But returns error in getimagesize(). "Parameter cannot be Empty"

1 answer

1


In this section of the original code,

$imagem = $_FILES["imagem"];

Modify to

$imagem = $_FILES['imagem']['tmp_name'];

The above example is considering an image only.
Multiple image upload requires a different treatment.

If you have questions about which indexes are in the array, do a print_r($_FILES); exit;. Then you can see the structure.

To give more consistency in switch()

 case "image/jpeg": 
       echo "Image is a jpeg"; 
       break;

Change to

 case "image/jpeg":
 case "image/pjpeg":
 case "image/jpg":
 case "image/pjpg":
       echo "Image is a jpeg"; 
       break; 

The reason is that jpg can come with one of these 4 formats.

Another point that needs to be more consistent is to check if the image exists before trying to use it.

if (file_exists($imagem)) {
    $size = getimagesize($imagem); 
} else {
    // dispara um exception, seta um código de erro.. enfim, o que vc preferir
}

There are many other details. I just showed the most obvious and simple.

The examples are purely didactic.

  • made a mistake Warning: getimagesize(102.jpg): failed to open stream: No such file or directory in /Library/WebServer/Documents/teste.php on line 8 line 8 is $imagem = $_FILES['imagem']['tmp_name'];

  • Strange because tmp_name does not return the original name. It returns the absolute path of the temporary file. The name is serialized, something like a md5 format. Clear the doubt by making a breakpoint with print_r() as commented in the reply.

  • updated my question with the new code. give a look there please

  • Do not modify the whole question. When so just add some new information below the question. I reversed and put the new information. But, for the third time, I ask you to put the breakpoint with print_r(). Without proper feedback I can’t continue.

  • excuse my ignorance, but where should I put the print_r();?

  • look I realized that I copy the image to the folder to where this php file, the error does not occur. it seems that the system only reads the image if it is on the server.

  • The image is on the server, otherwise the copy would not be possible.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.