在 PHP 中更正对文件上传字段的变量引用

Correct variable reference to a file upload field in PHP

我正在尝试成功使用 PHP empty() 函数。目前我的脚本引用了输入文件字段变量:

<input  class="fileUpload" name="flyer" type="file"  />

像这样:

if (empty($_FILES['flyer']['name'])) {
//don't update the file 
}
else {
//update the file url
}

这当前将文件 URL 更新为 null 的值(如果它为空)。问题可能出在其他地方,但我认为这是我的变量引用。我如何更正此问题才能使 empty 函数正常工作?

如果它是一个 FILE 类型,那么你需要使用 $_FILES[-name-]['error'] 处理程序来检查文件的质量,检查 http://php.net/manual/en/features.file-upload.errors.php

忽略name值,你要的是

if ($_FILES['flyer']['error'] == 4) {
//// this file given was empty. 
}

除了$_FILES['flyer']['error'],您还可以检查所需位置是否存在任何文件。

if(!file_exists($_FILES['flyer']['tmp_name']) || !is_uploaded_file($_FILES['flyer']['tmp_name'])) {
    echo 'No upload';
}

查看此 link 了解有关 is_uploaded_file()

的详细信息