php $_FILES["file"]["size"] 返回 0?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7959615/
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-26 03:41:53  来源:igfitidea点击:

$_FILES["file"]["size"] returning 0?

phpfile-upload

提问by user494216

I am trying to upload something using PHP and set a limit on the total size that I allow to be uploaded. I want to limit my uploads to 2MB but for some reason whenever I try to check with an if statement like this:

我正在尝试使用 PHP 上传内容并设置允许上传的总大小限制。我想将上传限制为 2MB,但出于某种原因,每当我尝试使用这样的 if 语句进行检查时:

if (($_FILES["file"]["size"] < 2097152))

A file that is large (such as a 7mb file) will pass through the if statement because for whatever reason if I print $_FILES["file"]["size"], it will return 0, instead of the proper number of bytes. If I try to upload something that is smaller, like 342kb the $_FILES["file"]["size"]will return the proper size.

大文件(例如 7mb 文件)将通过 if 语句,因为无论出于何种原因,如果我打印$_FILES["file"]["size"],它将返回 0,而不是正确的字节数。如果我尝试上传较小的内容,例如 342kb,$_FILES["file"]["size"]将返回正确的大小。

Is there anyway to get $_FILES["file"]["size"]to actually hold the proper size of the file? Otherwise I do not know how to fix this problem.

有没有$_FILES["file"]["size"]办法实际保存文件的正确大小?否则我不知道如何解决这个问题。

回答by Marc B

A file which aborts for any reason (upload failed, exceeds limits, etc...) will show as size 0

由于任何原因(上传失败、超出限制等)而中止的文件将显示为大小 0

You have to check for upload SUCCESS before you do ANYTHING with the rest of th eupload data:

在对其余的 eupload 数据执行任何操作之前,您必须检查上传是否成功:

if(array_key_exists('file', $_FILES)){
    if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
       echo 'upload was successful';
    } else {
       die("Upload failed with error code " . $_FILES['file']['error']);
    }
}

The error codes are defined here. In your case, if you've hardcoded a 2meg limit and someone uploads a 2.1 meg file, then the error code would be UPLOAD_ERR_INI_SIZE (aka 2), which is "exceeds limit set in .ini file".

错误代码在此处定义。在您的情况下,如果您硬编码了 2meg 限制并且有人上传了 2.1 meg 文件,那么错误代码将是 UPLOAD_ERR_INI_SIZE(又名2),即“超出 .ini 文件中设置的限制”。

回答by Niet the Dark Absol

if( $_FILES['file']['size'] && $_FILES['file']['size'] < (2<<20))

Try that.

试试那个。

<< is bitwise shift operator, decimal 2 is binary "10", then add 20 zeros.

<< 是按位移位运算符,十进制 2 是二进制“10”,然后添加 20 个零。

回答by Marco

How I supposed in my previous comment, your problem is that limit of uploadable file in php.ini is less than 7MB.
So you could try to use

我在之前的评论中如何假设,您的问题是 php.ini 中可上传文件的限制小于 7MB。
所以你可以尝试使用

if ($_FILES["file"]["size"] > 0 && $_FILES["file"]["size"] < 2097152)

Consider that if you put your limit (in php.ini) to 2MB, that check could be easily written as

考虑一下,如果您将限制(在 php.ini 中)设置为 2MB,则该检查可以轻松编写为

if ($_FILES["file"]["size"] > 0)

回答by I try so hard but I cry harder

check for any errors prior to uploading. These will often give away what the problem is. Create a class to return any code error and use it for uploading files.

上传前检查是否有任何错误。这些通常会泄露问题所在。创建一个类以返回任何代码错误并将其用于上传文件。

 class UploadException extends Exception
{
public function __construct($code) {
    $message = $this->codeToMessage($code);
    parent::__construct($message, $code);
}

private function codeToMessage($code)
{
    switch ($code) {
        case UPLOAD_ERR_INI_SIZE:
            $message = "The uploaded file exceeds the upload_max_filesize directive in php.ini.";
            break;
        case UPLOAD_ERR_FORM_SIZE:
            $message = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
            break;
        case UPLOAD_ERR_PARTIAL:
            $message = "The uploaded file was only partially uploaded";
            break;
        case UPLOAD_ERR_NO_FILE:
            $message = "No file was uploaded";
            break;
        case UPLOAD_ERR_NO_TMP_DIR:
            $message = "Missing a temporary folder";
            break;
        case UPLOAD_ERR_CANT_WRITE:
            $message = "Failed to write file to disk";
            break;
        case UPLOAD_ERR_EXTENSION:
            $message = "File upload stopped by extension";
            break;

        default:
            $message = "Unknown upload error";
            break;
    }
    return $message;
  }
}

Now check whether the upload was success before you do anything with the file. If it's NOT uploaded successfully, it'll result in a error which will tell you what's wrong.

现在检查上传是否成功,然后再对文件执行任何操作。如果它没有成功上传,它会导致一个错误,它会告诉你出了什么问题。

This way, instead of having to waste your time on guessing what the error code could mean or having to look it up all the time, your own made up error message will return the message corresponding with the error code.

这样,您不必浪费时间猜测错误代码可能意味着什么或必须一直查找它,您自己编写的错误消息将返回与错误代码对应的消息。

   if ($_FILES['realFile']['error'] === UPLOAD_ERR_OK) {
        echo 'no problems encountered. File was uploaded with success';
    } else {
        throw new UploadException($_FILES['realFile']['error']);
    }