PHP: What's the point of UPLOAD_ERR_INI_SIZE?

2019-03-03 12:00发布

The PHP manual has a section called Handling file uploads. That section has a subsection called Error Messages Explained. That subsection describes an error called "UPLOAD_ERR_INI_SIZE":

Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.

However, in my experience, it's impossible to ever check for this particular error using UPLOAD_ERR_INI_SIZE because if a user ever does upload a file that exceeds the upload_max_filesize directive in php.ini, the $_FILES superglobal is empty. Want to test it for yourself? Save this as "upload_test.php" and then try to upload a file that's under the limit and then a file that's over the limit:

<?php

    if (isset($_GET['submitted']) && $_GET['submitted'] === 'true')
    {
        echo 'Contents of $_POST:<hr><pre>';
        print_r($_POST);
        echo '</pre><hr>Contents of $_FILES:<hr><pre>';
        print_r($_FILES);
        echo '</pre><hr>';
        exit;
    }

    $max_filesize_in_mib = min((int)(ini_get('upload_max_filesize')), (int)(ini_get('post_max_size')), (int)(ini_get('memory_limit')));

?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>PHP Upload Test</title>
    </head>
    <body>
        <h1>Upload a File (Maximum File Size: <?php echo $max_filesize_in_mib; ?> MiB)</h1>
        <form action="upload_test.php?submitted=true" enctype="multipart/form-data" method="post">
            <input type="file" name="upload_test">
            <input type="hidden" name="random_field" value="You should see this field in the $_POST superglobal.">
            <input type="submit" value="Upload">
        </form>
    </body>
</html>

So my question is this: what's the point of UPLOAD_ERR_INI_SIZE if you can never check for it?

3条回答
狗以群分
2楼-- · 2019-03-03 12:23

UPLOAD_ERR_INI_SIZE Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.

This make sense when your POST_MAX_SIZE is bigger than UPLOAD_MAX_FILESIZE.

I tried in an environment where POST_MAX_SIZE is 128MB, then i set UPLOAD_MAX_FILESIZE to 1MB

Here's what i got (as expected):

Contents of $_POST:
Array
(
    [random_field] => You should see this field in the $_POST superglobal.
)

Contents of $_FILES:
Array
(
    [upload_test] => Array
        (
            [name] => Scan.tiff
            [type] => 
            [tmp_name] => 
            [error] => 1
            [size] => 0
        )
)

Although we don't get the size of the file, we do know that it's exceeding the upload_max_filesize.

查看更多
放我归山
3楼-- · 2019-03-03 12:26

As with so many things, this constant can be traced back to a constant in the PHP source. In particular, line 66 in rfc1867.c.

It would seem that perhaps the person documenting the upload functionality was just copying and explaining each constant they saw registered in the source.

查看更多
时光不老,我们不散
4楼-- · 2019-03-03 12:45

Is to track the error when it The uploaded file exceeds the upload_max_filesize directive in php.ini.

You can track the error using error_get_last, or the try .. catch

查看更多
登录 后发表回答