问题
How can I tell the difference in PHP between the user not selecting a file, and an upload error occurring?
Thanks
回答1:
if a user has not selected a file the field would be empty you should be able to to error checking
if($_FILES["file"]["name"] != "") { THERE IS A FILE HERE } ELSE { ERROR USER DID NOT SELECT FILE }
回答2:
If there was an error uploading, there will be a filename. If there's no filename or no $_FILES
array, the user didn't select a file.
回答3:
If I am not mistaken I believe you are referring to this
$upload_errors = array(
UPLOAD_ERR_OK =>"No errors.",
UPLOAD_ERR_INI_SIZE => "Larger than upload_max_filesize.",
UPLOAD_ERR_FORM_SIZE => "Larger than form MAX_FILE_SIZE.",
UPLOAD_ERR_PARTIAL => "Partial upload.",
UPLOAD_ERR_NO_FILE => "No file.",
UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.",
UPLOAD_ERR_CANT_WRITE => "Can't write to disk.",
UPLOAD_ERR_EXTENSION => "File upload stopped by extension."
);
$tmp_name = $_FILES['file_upload']['tmp_name'];
$target_file = basename($_FILES['file_upload']['name']);
$upload_dir = "uploads";
if(move_uploaded_file($tmp_name, $upload_dir."/".$target_file)){
$message = "File uploaded successfully.";
}else{
$error = $_FILES['file_upload']['error'];
$message = $upload_errors[$error];
}
来源:https://stackoverflow.com/questions/13042362/php-file-upload-determine-error-or-no-file-selected