php 如何检查文件输入字段是否为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10096977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if the file input field is empty?
提问by Mohamed Hassan
I am having a hard time using $_FILES
我很难使用 $_FILES
I want to check if file upload field is empty or not then apply a condition such that if file upload is empty then the script doesn't try uploading the file. How do I enforce this?
我想检查文件上传字段是否为空,然后应用一个条件,如果文件上传为空,则脚本不会尝试上传文件。我如何强制执行此操作?
回答by Mohamed Hassan
if($_FILES["file"]["error"] != 0) {
//stands for any kind of errors happen during the uploading
}
also there is
还有
if($_FILES["file"]["error"] == 4) {
//means there is no file uploaded
}
回答by Drazen Mokic
This should work
这应该工作
if ( ! empty($_FILES)) {...}
回答by Mageek
The other answers didn't work for me. So I post my solution:
其他答案对我不起作用。所以我发布了我的解决方案:
if($_FILES['theFile']['name']=='')
{
//No file selected
}
回答by bash3r
Here's what worked for me:
以下是对我有用的内容:
if ($_FILES['theFile']['tmp_name']!='') {
// do this, upload file
} // if no file selected to upload, file isn't uploaded.
回答by lombervid
You can use the UPLOAD_ERR_NO_FILEvalue:
您可以使用UPLOAD_ERR_NO_FILE值:
function isset_file($file) {
return (isset($file) && $file['error'] != UPLOAD_ERR_NO_FILE);
}
if(isset_file($_FILES['input_name'])) {
// It's not empty
}
Updated:Since sending $_FILES['input_name']may throw a Notice
更新:由于发送$_FILES['input_name']可能会抛出一个通知
function isset_file($name) {
return (isset($_FILES[$name]) && $_FILES[$name]['error'] != UPLOAD_ERR_NO_FILE);
}
if(isset_file('input_name')) {
// It's not empty
}
回答by Amir
this question is duplicate but your answer is is_uploade_file()function.
这个问题是重复的,但你的答案是is_uploade_file()函数。
回答by evilReiko
if(!empty($_FILES['myFileField'])) {
// file field is not empty..
} else {
// no file uploaded..
}
回答by readytolearn
To check if an input of type file is emptyyou will have to take any of $_FILESarrays and check it against an empty array. All that I have seen above is check against an empty string which will not work.
Example:
要检查文件类型的输入是否为空,您必须采用任何$_FILES数组并根据空数组检查它。我在上面看到的只是检查一个无效的空字符串。
例子:
if($_FILES["your_field_name"]["size"] == [' '])
{
Perform your validation here?
}
I hope this helps.
我希望这有帮助。

