doushi9376 2011-11-28 18:13
浏览 274
已采纳

PHP: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?

  • 写回答

3条回答 默认 最新

  • dtxpz8785 2011-11-28 18:30
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题