Getimagesize() returns read error Notice, just want it to return false

As part of many checks to make sure an uploaded image is in fact, an image, and not something else in disguise, I use the following on uploaded files.

$imageinfo = getimagesize($_FILES[‘uploaded_file’][‘tmp_name’]);

But it is giving me a PHP Notice "Notice: getimagesize(): Read error! " when the file is not an image. Knowing it is not an image is exactly what I want to know, which is why I check to see if $imageinfo returned false or not, which is valuable information to me. But I don’t want to have to suppress the notice with @ if there is another solution since many say that practice should be avoided. Any ideas on how to let getimagesize() do its thing on files that aren’t images so I can get the expected return of “false” on them?

Why don’t you use a regular if-else?

Just check if the function gives you an error and if it does, execute the code accordingly. And if it doesn’t, well, you already know what to do :slight_smile:

I don’t understand how to use a regular if/else here to avoid the notice.

if (getimagesize($_FILES[‘uploaded_file’][‘tmp_name’]))
{
$imageinfo = getimagesize($_FILES[‘uploaded_file’][‘tmp_name’]);
}
else
{
echo ‘error’;
}

Still provides the same php notice, even if “error” is echoed.

Substitute exif_imagetype for getimagesize in the if statement. exifimagetype either retirns a value indicating the type of the image (determined from examining the content of the image itself) or false if it is not an image

http://php.net/manual/en/function.exif-imagetype.php

2 Likes

I did some research on that method and it looks like a good alternative. Thanks for suggesting it.

9 posts were split to a new topic: Exifimagetype() returns read error Notice, just want it to return false