PHP文件上载确定错误或没有选择文件

时间:2012-10-24 02:57:22

标签: php upload

如何判断用户未选择文件与发生上传错误之间的区别?

由于

3 个答案:

答案 0 :(得分:0)

如果上传时出错,则会有文件名。如果没有文件名或没有$_FILES数组,则用户没有选择文件。

答案 1 :(得分:0)

如果我没有弄错,我相信你指的是这个

$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];
}

答案 2 :(得分:0)

如果用户未选择文件,该字段将为空,您应该能够进行错误检查

if($_FILES["file"]["name"] != "") { THERE IS A FILE HERE } ELSE { ERROR USER DID NOT SELECT FILE }