6

在 PHP 中上传大文件时遇到一个奇怪的问题。

在php.ini中,max_execution_time设置为30,post_max_size设置为32M,upload_max_filesize设置为32M。当我尝试上传大小为 40.2 MB 的文件时,它没有显示任何错误。该$_FILES变量具有值array(0) { }$_FILES['userfile']显示NULL

如果文件大小超过 php.ini 中设置的值,那么它应该返回错误消息

UPLOAD_ERR_INI_SIZE,值:1;上传的文件超过了 php.ini 中的 upload_max_filesize 指令。

但它也没有显示任何错误($_FILES空数组也是如此)。我不知道为什么会这样。

当我更改 php.ini 并将 post_max_size 设置为 64M,upload_max_filesize设置为 64M 时,它工作正常。因此,我决定使用以下代码,而不是更改 php.ini 文件。

ini_set('upload_max_filesize', '64M');
ini_set('post_max_size', '64M');
ini_set('max_execution_time', 300);

我什至试图增加max_execution_time. 不过,我有同样的问题。ini_set()不在这里工作。

4

4 回答 4

20

To have the 40 MB file fail with upload error, you have to increase the post_max_size, if you exceed the value of that variable, you get an empty $_FILES array. See the manual

If the size of post data is greater than post_max_size, the $_POST and $_FILES superglobals are empty.

Also, ini_set() is not working there because two of the variables you are trying to change are PHP_INI_PERDIR and thus need to be changed in php.ini or in .htaccess or httpd.conf. You should try the 40MB file with, for example, these settings in .htaccess

php_value upload_max_filesize 32M
php_value post_max_size 64M
php_value max_execution_time 300
于 2010-08-22T20:28:12.463 回答
3

There is one more setting you may need to look at, Apache's LimitRequestBody.

If the file exceeds that, the upload may get blocked before it even reaches PHP.

Apache Documentation

于 2010-08-22T20:28:33.650 回答
2

ini_set() 在这里不起作用。

您试图用 更改的值ini_set(),除了max_execution_time,不能用 更改ini_set()
php.ini 指令列表中,它们被报告为 type PHP_INI_PERDIR,这意味着(如可以设置配置设置的位置中所述)它们可以在 php.ini、.htaccess 或 httpd.conf 中更改。可以更改的配置设置是ini_set()标记为 的设置PHP_INI_USER

于 2010-08-22T20:43:12.113 回答
0

对于服务器:

在 cPanel 中搜索 php,您将在 Software 下找到“Select PHP version”。软件 -> 选择 PHP 版本 -> 切换到 PHP 选项 -> 更改值 -> 保存。

本地:

在 xampp 下的 php 文件夹中找到 PHP ini(配置设置)文件。改变 post_max_size = 40Mupload_max_filesize = 40M

于 2018-03-02T10:03:46.340 回答