我有一个网站托管在我无法访问的PC上。我有一个上传表单,允许人们上传最大30MB的mp3文件。我的服务器端脚本是用PHP完成的。

每次我尝试上传文件时,都会收到一个错误,声称文件超过了允许的最大大小,因此我需要增加大小。我在网络上的研究建议更改。htaccess文件,我没有访问权限,所以这将不起作用。其他人建议我添加一个自定义的php.ini文件到我的根目录,但这并不奏效。还有其他建议吗?


当前回答

你需要检查三件事。

php.ini配置文件中的Upload_max_filesize, memory_limit和post_max_size。

所有这三个设置都限制了PHP可以提交和处理的数据的最大大小。

通常post_max_size和memory_limit需要大于upload_max_filesize。


所以你总共需要检查三个变量才能完全确定。

其他回答

有了WAMP,一切都很简单

WAMP图标> PHP > PHP设置> upload_max_filesize = nM > n = (2M, 4M, 8M, 16M, 32M, 64M, 128M, 256M, 512M,或选择(自定义))。

服务自动重新加载。

但是,如果您确实没有访问服务器的权限,您可能想要尝试编写一个分块API。

下面是如何做到这一点的图片。

现有的答案都有部分解决方案,所以这里是编译的答案:

解决方法一:编辑。htaccess文件 适用于Apache服务器

php_value upload_max_filesize 128M
php_value post_max_size 132M
php_value memory_limit 256M
php_value max_execution_time 300
php_value max_input_time 300

解决方案2:编辑wp-config.php文件 适合Wordpress应用程序

@ini_set( 'upload_max_filesize' , '128M' );
@ini_set( 'post_max_size', '132M');
@ini_set( 'memory_limit', '256M' );
@ini_set( 'max_execution_time', '300' );
@ini_set( 'max_input_time', '300' );

解决方案3:编辑php.ini文件 适用于nginx服务器或php.ini可修改的地方

upload_max_filesize = 128M
post_max_size = 132M
memory_limit = 256M
max_execution_time = 300
max_input_time = 300

可选:Nginx服务器 对于nginx增加最大文件上传大小限制(默认1MB),增加client_max_body_size 128M;HTTP或服务器块中的指令。

设置的重要说明:

upload_max_filesize – set this to a value > than your file size post_max_size – set this to a value > than your upload_max_filesize because overall size of the posted fields may be higher than the filesize memory_limit – set this to a value > than your file size because it limits the maximum amount of memory a script may consume max_execution_time – Maximum execution time of each script, in seconds. Set this to 0 (infinite) if your script requires long time to process file max_input_time - Maximum amount of time each script may spend parsing request data. Default is 60 seconds. Set this to -1 if scripts requires more time

最后,不要忘记重新启动服务器。适用于以下内容:

# if php-fpm is used for processing php
sudo service php7.4-fpm restart

# for nginx
sudo service nginx restart

# for apache
sudo service httpd restart

我有同样的问题,我创建了一个.user.ini文件,并把它放在上传脚本所在的目录中。然后在文件中我设置了这两个值:

  upload_max_filesize = 40M

  post_max_size = 40M

这对我来说非常有效!

我知道这个问题有很多答案,但在php.ini中,大多数变量只用于更改最大上传文件大小

由于有时用户更新了最大文件大小,但没有更新内存限制,导致中断导入过程。

因此,我在这里列出了所有需要更改的相关变量以及最大文件大小

max_execution_time memory_limit display_errors post_max_size upload_max_filesize max_input_time


注意:从现在开始,ini存储在这样的文件夹中 /etc/php/8.1/apache2/php.ini

好吧,我想在这里补充我的意见。

I'm using shared webhosting and I tackled this problem many times, tried to resolve it on my own but to no avail. Finally I managed to resolve it through checking various web sources and contacting my hosting service provider. My questions were "How can I change php value memory_limit in shared webhosting?", "How can I change php value upload_max_filesize in shared webhosting?", "How can I change php value max_input_vars in shared webhosting?", "How can I change php value max_execution_time in shared webhosting?", "How can I change php value max_input_time in shared webhosting?" and many more by configuring or changing php.ini or .htaccess file. I tried to change them but problems arose. Finally I contacted my hosting provider, and it turns out that I set my php to native, they changed it to php 5.6, here is their answer:

“你的PHP被设置为‘原生’模式,这意味着你不能重写 这些值。我已经把你的身高改成5.6了,所以你应该可以 走吧。”

之后,我通过ftp Filezilla连接我的网站,也别忘了让你的ftp服务显示隐藏文件,和你的本地计算机这样做,因为。htaccess文件隐藏在我的本地笔记本电脑和我的网站。它可以在public_html文件夹中找到,我只是下载了它,并在文件末尾添加了以下代码,然后将其上传到服务器:

php_value memory_limit 256M
php_value post_max_size 256M
php_value upload_max_filesize 64M
php_value max_input_vars 1800
php_value max_execution_time 300
php_value max_input_time 300

暂时一切正常,如果有什么问题请写在这里并警告我,我可以更改上面显示的代码。顺便说一下,我也上传了一些图片,展示了变化。

还有一件事,我几乎忘记了在你的共享网络托管服务上安装ZipArchive,我通过通过我的cpanel进入php设置,单击php选择器扩展,然后勾选zip部分,这就是全部。

谢谢。

PS:我对好的做法持开放态度,如果你在这里看到任何不好的做法,请告诉我,我会尝试改变它们。谢谢。