问题
I am having a hard time using $_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?
回答1:
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
}
回答2:
This should work
if ( ! empty($_FILES)) {...}
回答3:
The other answers didn't work for me. So I post my solution:
if($_FILES['theFile']['name']=='')
{
//No file selected
}
回答4:
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.
回答5:
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
}
回答6:
this question is duplicate but your answer is is_uploade_file() function.
回答7:
if(!empty($_FILES['myFileField'])) {
// file field is not empty..
} else {
// no file uploaded..
}
来源:https://stackoverflow.com/questions/10096977/how-to-check-if-the-file-input-field-is-empty