Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
618 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...