我在用 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, Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.
但它也没有显示任何错误(因为 $_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()
在这里不起作用。
最佳答案
要使 40 MB 的文件因上传错误而失败,您必须增加 post_max_size,如果超过该变量的值,您将得到一个空的 $_FILES 数组。参见 the manual
If the size of post data is greater than post_max_size, the $_POST and $_FILES superglobals are empty.
此外,ini_set() 在那里不起作用,因为您尝试更改的两个变量是 PHP_INI_PERDIR因此需要在 php.ini 或 .htaccess 或 httpd.conf 中进行更改。您应该尝试使用 40MB 的文件,例如 these settings in .htaccess
php_value upload_max_filesize 32M
php_value post_max_size 64M
php_value max_execution_time 300
关于php - 上传大文件时出现奇怪的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3543194/