Warning: getimagesize() [function.getimagesize]: Filename cannot be empty warning message?

user2284433 picture user2284433 · Apr 16, 2013 · Viewed 31.5k times · Source

Up until recently I've been using some PHP to upload photos to a site. But suddenly it's started triggering all sorts of error messages.

I use a form that on action runs the following code:

$uploaddir = "../../user_content/photo/";
$allowed_ext = array("jpeg", "jpg", "gif", "png");

    if(isset($_POST["submit"])) {
        $file_temp = $_FILES['file']['tmp_name'];   
        $info = getimagesize($file_temp);
    } else {
        print "File not sent to server succesfully!";
        exit;
    }

The file upload part of the form has the following elements:

<input name="file" type="file" class="photo_file_upload">

The submit button for uploading has the following attributes:

<button type="submit" name="submit" class="photo_upload">Upload</button>

But whenever I run this, I always get the following warning:

Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in (upload PHP file) on line 10

(line 10 is this part: $info = getimagesize($file_temp);)

Anyone have any ideas on what the cause of this is?

Answer

Yaroslav picture Yaroslav · Apr 16, 2013

You checked if the form was submitted, but didn't check if the file was sent. In some cases, a form could be submitted but the file will not sent (i.e. file size is bigger then file size limit in config).

Use this:

if(isset($_POST["submit"]) && isset($_FILES['file'])) {
    $file_temp = $_FILES['file']['tmp_name'];   
    $info = getimagesize($file_temp);
} else {
    print "File not sent to server succesfully!";
    exit;
}

You see && isset($_FILES['file']) is new

Or you can extend it

if(isset($_POST["submit"]) && isset($_FILES['file'])) {
    $file_temp = $_FILES['file']['tmp_name'];   
    $info = getimagesize($file_temp);
} 
elseif(isset($_POST["submit"]) && !isset($_FILES['file'])) {
    print "Form was submitted but file wasn't send";
    exit;
}
else {
    print "Form wasn't submitted!";
    exit;
}