Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

file upload - PHP: What's the point of UPLOAD_ERR_INI_SIZE?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...