good_evening
good_evening

Reputation: 21759

Why 'if(!getimagesize($_FILES['imagefile']['tmp_name']))' doesn't work?

After the file is uploaded, why it always gives me the false even if it is image?

if (!getimagesize($_FILES['imagefile']['tmp_name'])) { $boolean = false; }

By the way, it gives me this error:
Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in...

Upvotes: 2

Views: 4889

Answers (2)

Marc B
Marc B

Reputation: 360762

First check if the upload is actually succeeding:

if ($_FILES['imagefile']['error'] === UPLOAD_ERR_OK) {
   if (!getimagesize(....)) {
      ... 
   }
} else {
   die("Upload failed with error code {$_FILES['imagefile']['error']}");
}

The error constants are defined here. Never assume an upload succeeded. There's only one way for them to work, and a million ways for them to fail.

Given that getimagesize() is complaining about an empty file name, either:

a. the upload failed, the reason code for which will be in the ...['error'] attribute.

b. you're checking the wrong file field name. If you've got <input type="file" name="image" /> then you have to check $_FILES['image'][...].

c. for whatever reason, your web server is able to WRITE files to the temporary directory, but does not have READ permissions.

Upvotes: 1

Sarfraz
Sarfraz

Reputation: 382806

Make sure that your file is being uploaded before carrying any operation on it. Just dump the $_FILES array while development, like:

echo '<pre>'; print_r($_FILES);echo '</pre>';  

You need to have a enctype attribute applied on your <form> tag, for uploading a file. See http://www.w3.org/TR/html401/interact/forms.html#h-17.3

Upvotes: 1

Related Questions