PHP测试表单是否已上传文件

时间:2012-04-17 20:10:52

标签: php forms

我该怎么办?这些不起作用:

if(empty($_FILES)){
  echo "there is no file uploaded";
  exit;
} else {
  echo "there is file";
}

if(sizeof($_FILES)!=0){
  echo "there is file";
} else {
  echo "there is no file";
}

3 个答案:

答案 0 :(得分:9)

尝试

示例:

<?php

if (isset ( $_FILES ['image'] )) {
    if ($_FILES ["image"] ["error"] == UPLOAD_ERR_OK) {
        echo "File Was Uploaded AND OK";
    } else {
        echo "File Was Uploaded but BAD";
    }
} else {
    echo "No File Uploaded";
}

?>

<form method="post" enctype="multipart/form-data">
    <label for="file">Filename 1:</label> <input type="file" name="image"
        id="file" /> <br /> <input type="submit" name="submit" value="Submit" />
</form>

您可以在此处查看更多示例http://php.net/manual/en/function.move-uploaded-file.php

答案 1 :(得分:1)

来自:http://php.net/manual/en/reserved.variables.files.php

“如果$ _FILES为空...尝试将enctype="multipart/form-data"添加到表单标记,并确保已启用文件上传。”

答案 2 :(得分:1)

你可以使用is_array($ _ FILES)检查它是否包含数组格式的文件信息