PHP 检查是否有选择要上传的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26432476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP check if there is a file selected for upload
提问by Mae
I have this form and I would like to check if the user has selected a file or not.
我有这个表格,我想检查用户是否选择了一个文件。
<form action="upload.php" method="POST" enctype="multipart/form-data" >
<select name="category">
<option value="cat1" name="cat1">Productfotografie</option>
<option value="cat2" name="cat2">Portretten</option>
<option value="cat3" name="cat3">Achitectuur</option>
</select>
<input type="file" name="file_upload">
<input type="submit" name="submit" value="Upload photo">
</form>
I wrote this PHP code to test it
我写了这个 PHP 代码来测试它
if (empty($_POST) === false) {
$fileupload = $_POST['file_upload'];
if (empty($fileupload) === true) { //
echo "Error no file selected";
} else {
print_r($_FILES);
}
}
But I get the "Error no file selected" even if I DO select something. Any clue? I'm sorry I'm really new in PHP.
但是即使我确实选择了某些内容,我也会收到“未选择文件的错误”。有什么线索吗?对不起,我真的是 PHP 新手。
EDIT:I already tried replacing $fileupload = $_FILES['file_upload']
but it prints an empty error
编辑:我已经尝试更换,$fileupload = $_FILES['file_upload']
但它打印一个空错误
(Array ( [file_upload] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) ))
(数组 ( [file_upload] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0)))
when I do NOT enter a file?
当我不输入文件时?
回答by MrCode
Use the $_FILES
array and the UPLOAD_ERR_NO_FILE
constant:
使用$_FILES
数组和UPLOAD_ERR_NO_FILE
常量:
if(!isset($_FILES['file_upload']) || $_FILES['file_upload']['error'] == UPLOAD_ERR_NO_FILE) {
echo "Error no file selected";
} else {
print_r($_FILES);
}
You can also check UPLOAD_ERR_OK
which indicates if the file was successfully uploaded (present and no errors).
您还可以检查UPLOAD_ERR_OK
哪个指示文件是否已成功上传(存在且没有错误)。
Note: you cannot use empty()
on the $_FILES['file_upoad']
array, because even if no file is uploaded, the array is still populated and the error
element is set, which means empty()
will return false
.
注意:不能empty()
在$_FILES['file_upoad']
数组上使用,因为即使没有上传文件,数组仍然填充并设置error
元素,这意味着empty()
将返回false
.
回答by antelove
First, click submit without choose file
首先,点击提交而不选择文件
/* check input 'submit' */
if (isset($_POST['submit'])) {
print($_FILES);
}
Result:
结果:
> Array ( > [file_upload] => Array > ( > [name] => > [type] => > [tmp_name] => > [error] => 4 > [size] => 0 > ) > > )
There are array of [file_upload][error] = 4, NOT null or whitespace.
Code value = 4 , meaning UPLOAD_ERR_NO_FILE.
If success and no error, code value = 0.
Check here
So use function isset, not empty
有[file_upload][error] = 4数组,非空或空格。
代码值 = 4 ,表示 UPLOAD_ERR_NO_FILE。
如果成功并且没有错误,代码值= 0。在这里
检查所以使用函数isset,而不是空
/* check input submit */
if (isset($_POST['submit'])) {
/* check input file_upload on error */
if (isset($_FILES['file_upload']['error'])) {
/* check code error = 4 for validate 'no file' */
if ($_FILES['file_upload']['error'] == 4) {
echo "Please select an image file ..";
}
}
}
回答by Matthias
According to http://www.php.netthere is $_FILES available since php 4.1.0. This variable keeps information about uploaded files. So you should code
根据http://www.php.net,从 php 4.1.0 开始就有 $_FILES 可用。此变量保存有关上传文件的信息。所以你应该编码
$fileupload = $_FILES['file_upload'];
if (isset($fileupload)) {
print_r($_FILES);
} else {
echo "Error no file selected";
}