views:

330

answers:

4

I'm running getimagesize() to determine whether or not an uploaded file is a valid image and also it's MIME type. A user of my webapp reported that some of his images where getting an "invalid filetype" error after uploading them. So I asked him to send me some of them to check. Here's one.

The error string itself is meaningless as it's just a generic text written by me when the functions fails (returns FALSE). I tried upping PHP's memory limit with no success. Also, logs don't show anything unusual. Is there a problem with the files or is it something else? is there a better way to do this operation?

I'm accepting any file that getimagesize() accepts (meaning it has a width/height), I'm just checking that it doens't return FALSE (although my real scope would be jpg|jpeg|png|gif|bmp|tif|tiff). Server's running PHP/5.2.6-1+lenny3. I repeat, this happens only with the image linked and some others from the same series, so I'm more inclined to think it's related to what Lizard is hinting.

UPDATE: $_FILES seems to be empty before getting to getimagesize($_FILES['Filedata']['tmp_name']) so the file is never really checked, I'll have to figure out why these files are not submitted like the rest (too big for PHP perhaps? any ideas?). I have to go now but I'll be checking this later, any help is welcome. Thanks!

A: 

I am not sure if this is the answer your looking for but I know that I have issues with php functions and images being over 3000px in any dimension.

Hope that helps!

Lizard
+3  A: 

Until you give more information, I can't really help much. The following code:

$file = "http://img27.imageshack.us/img27/9159/dsc00799e.jpg";
$info = getimagesize($file);

echo "<pre>" . print_r($info,true) . "</pre>";

(Where $file is equal to the location of the image given in the question), outputs:

Array
(
    [0] => 2736
    [1] => 3648
    [2] => 2
    [3] => width="2736" height="3648"
    [bits] => 8
    [channels] => 3
    [mime] => image/jpeg
)

This seems okay. I'm assuming an error somewhere else in your checks?

David Thomas
Yeah, same result here with PHP 5.2.3
pablasso
I get the same results too, so I'm updating the question, thanks ricebowl and pablasso!
fandelost
A: 

After looking around SO I've found this insightful answer regarding PHP's upload limits. I combined it with what I already knew and finally got it working using the following code in my .htaccess (limiting uploads to 5MB):

php_value upload_max_filesize 5M
php_value post_max_size 8M
php_value max_input_time 1800
LimitRequestBody 0

Bottom line is: the problem wasn't in PHP's getimagesize() function, it was that the images were never uploaded to the server due to file size limits in both Apache and PHP. Thanks everyone who answered and commented, It would have been a lot more painful to figure it out without your help!

fandelost
A: 

You said

$_FILES seems to be empty

Did you check for an upload error? - When the upload fails PHP sets the error element of the file's array. See the PHP documentation's File Upload section for different error codes possible.

johannes
Thank you for your answer johannes, however the file wasn't even getting to the script since PHP couldn't handle it due to the imposed upload limits. As I said in my answer, changing these limits made it work fine.
fandelost