如何在PHP中上传大于2MB的文件?我在互联网上搜索并更改了
php.ini
文件,修改了以下行:"upload_max_filesize = 200M"
,但我仍然无法上传超过2MB的文件。看起来出现了什么问题?请帮帮我。提前感谢您的帮助。php.ini
文件,修改了以下行:"upload_max_filesize = 200M"
,但我仍然无法上传超过2MB的文件。看起来出现了什么问题?请帮帮我。提前感谢您的帮助。从前,我遇到了WAMP服务器的问题,当我寻找解决方案时,我偶然发现了这个讨论。如果有人遇到相同的问题,这是我的解决方法,希望能帮到你:
我使用的是WAMP堆栈。通过阅读您上面的评论,您也在使用WAMP堆栈。如果您不知道,WAMP服务器有两个php.ini(在PHP目录和Apache目录中)配置,一个用于CLI,另一个用于Apache本身(参见WAMP服务器上的php.ini)。因此,我创建了info.php来检测服务器使用的是哪个php.ini,在我的情况下,那是Apache目录中的那个(参见我的WAMP网页使用哪个PHP Ini文件?)。
打开服务器使用的php.ini,并像@Pascal Martin建议的那样更改upload_max_filesize
,并设置post_max_size
,然后重新启动服务器。
再次检查您的info.php,确保upload_max_filesize
和post_max_size
的值已更改为您需要的值。
重新启动Apache。
这对我有用,希望能帮助到你。
upload_max_filesize
... 但是,您还需要设置 post_max_size
(引用):upload_max_filesize
。phpinfo()
函数表示什么? - Pascal MARTIN还有其他选项可以限制这个:
max_input_time = 600
php_value max_execution_time = 600
post_max_size = 200M
(...并重新启动Apache)
获取您的php.ini-dist文件,
上传大文件(>5MB),我使用分块上传方法。
/**
* @param $file
* @param $fileSize
* @param $name
* @return int
*/
public function chunkUpload($file, $fileSize, $applicantID, $name) {
$targetFile = 'upload/'. $name;
$chunkSize = 256; // chunk in bytes
$uploadStart = 0;
$handle = fopen($file, "rb");
$fp = fopen($targetFile, 'w');
# Start uploading
try {
while($uploadStart < $fileSize) {
$contents = fread($handle, $chunkSize);
fwrite($fp, $contents);
$uploadStart += strlen($contents);
fseek($handle, $uploadStart);
}
fclose($handle);
fclose($fp);
return 200;
} catch (\Exception $e) {
return 400;
}
}
php_value upload_max_filesize 200M
php_value post_max_size 200M
php_value max_input_time 2000
以这种方式更改了服务器设置...
memory_limit = 250M //The maximum amount of memory in bytes a script is allowed to allocate.
max_input_time = 600 //The maximum time in seconds a script is allowed to parse input data.
max_execution_time = 600 //The maximum time in seconds a script is allowed to run before it is terminated.
post_max_size = 200M //The maximum size in bytes of data that can be posted with the POST method. Typically, should be larger than upload_max_filesize and smaller than memory_limit.
upload_max_filesize = 100M //The maximum size in bytes of an uploaded file.
如果要上传较大的文件,则需要从php.ini文件中更改/增加post_max_size和upload_max_filesize指令的值。
upload_max_filesize = 200M
post_max_size = 201M
这将将单个文件的上传限制从默认的2 MB增加到200 MB。
尝试在 PHP 脚本中设置它(在顶部)。
ini_set("upload_max_filesize", "255M");
ini_set("post_max_size, "256M");