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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:54:16  来源:igfitidea点击:

PHP check if there is a file selected for upload

phppost

提问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 $_FILESarray and the UPLOAD_ERR_NO_FILEconstant:

使用$_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_OKwhich 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 errorelement 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";     
}