编程语言
首页 > 编程语言> > php – 警告:getimagesize()[function.getimagesize]:文件名不能为空警告消息?

php – 警告:getimagesize()[function.getimagesize]:文件名不能为空警告消息?

作者:互联网

直到最近,我一直在使用一些PHP将照片上传到网站.但突然间它开始触发各种错误信息.

我使用on action的表单运行以下代码:

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

表单的文件上载部分包含以下元素:

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

用于上载的提交按钮具有以下属性:

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

但每当我运行它时,我总会收到以下警告:

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

(第10行是这部分:$info = getimagesize($file_temp);)

任何人对这是什么原因都有任何想法?

解决方法:

您检查了表单是否已提交,但未检查文件是否已发送.在某些情况下,可以提交表单但不会发送文件(即文件大小大于配置中的文件大小限制).

用这个:

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

你看&& isset($_ FILES [‘file’])是新的

或者你可以扩展它

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

标签:php,warnings,getimagesize
来源: https://codeday.me/bug/20191003/1846824.html