文件太大时上传不正确

2021-12-24 00:00:00 file-upload php

我有一个 PHP 应用程序,我可以在其中上传文件.当我上传大多数文件并执行 print_r($_FILES) 时,我得到如下信息:

I have a PHP app where I can upload files. When I upload most files and do a print_r($_FILES), I get something like this:

Array
(
    [import] => Array
        (
            [name] => Array
                (
                    [excel_file] => COD MKTG 2.csv
                )

            [type] => Array
                (
                    [excel_file] => application/vnd.ms-excel
                )

            [tmp_name] => Array
                (
                    [excel_file] => /tmp/phpy8mEKn
                )

            [error] => Array
                (
                    [excel_file] => 0
                )

            [size] => Array
                (
                    [excel_file] => 1584286
                )

        )

)

我有另一个 13 兆字节的 CSV 文件,当我尝试上传它时,我得到了这个:

I have another CSV file that's more like 13 megabytes, and when I try to upload that, I get this:

Array
(
    [import] => Array
        (
            [name] => Array
                (
                    [excel_file] => COD MKTG.csv
                )

            [type] => Array
                (
                    [excel_file] => 
                )

            [tmp_name] => Array
                (
                    [excel_file] => 
                )

            [error] => Array
                (
                    [excel_file] => 1
                )

            [size] => Array
                (
                    [excel_file] => 0
                )

        )

)

我没有收到任何说文件太大的错误.我只是得到一个格式错误的 $_FILES.我将 php.ini 中的 post_max_size 设置为 100MB.为什么会发生这种情况?

I don't get any error saying the file's too big. I just get a malformed $_FILES. I have post_max_size in php.ini set to 100MB. Why is this happening?

推荐答案

根据 PHP 文档,错误代码 1 是 UPLOAD_ERR_INI_SIZE: "上传的文件超过了 php.ini 中的 upload_max_filesize 指令"

As per the PHP docs, error code 1 is UPLOAD_ERR_INI_SIZE: "The uploaded file exceeds the upload_max_filesize directive in php.ini"

您需要确保正确设置以下所有变量:

You need to make sure all the following variables are properly set:

upload_max_filesize - 任何文件的最大大小上传中的单个文件
max_file_uploads - 允许的文件总数上传
post_max_size - 发布的所有数据的总和(表单数据+文件)
memory_limit - 必须 > post_max_size,以便为PHP + 脚本开销

upload_max_filesize - max size of any individual file in an upload
max_file_uploads - total number of files allowed to be uploaded
post_max_size - sum total of all data being POSTed (form data + files)
memory_limit - must be > post_max_size, to allow space for PHP + script overhead

除此之外,还有网络服务器限制.Apache 的 LimitRequestBody 早在 PHP 出现之前就适用了.

And on top of that, there's the web server limits as well. Apache's got LimitRequestBody which would apply long before PHP ever enters the picture.

相关文章