how to check if the file field is empty?

2019-01-25 05:37发布

I am having a hard time using $_FILES

I want to check if it's empty or not and if it's not empty then it shouldn't try and uploading the file. How do I check this?

7条回答
▲ chillily
2楼-- · 2019-01-25 05:41

this question is duplicate but your answer is is_uploade_file() function.

查看更多
smile是对你的礼貌
3楼-- · 2019-01-25 05:48

This should work

if ( ! empty($_FILES)) {...}
查看更多
时光不老,我们不散
4楼-- · 2019-01-25 05:49
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
}
查看更多
劫难
5楼-- · 2019-01-25 05:51
if(!empty($_FILES['myFileField'])) {
    // file field is not empty..
} else {
    // no file uploaded..
}
查看更多
爷的心禁止访问
6楼-- · 2019-01-25 06:02

You can use the UPLOAD_ERR_NO_FILE value:

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

function isset_file($name) {
    return (isset($_FILES[$name]) && $_FILES[$name]['error'] != UPLOAD_ERR_NO_FILE);
}

if(isset_file('input_name')) {
    // It's not empty
}
查看更多
来,给爷笑一个
7楼-- · 2019-01-25 06:03

The other answers didn't work for me. So I post my solution:

if($_FILES['theFile']['name']=='')
{
    //No file selected
}
查看更多
登录 后发表回答