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

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


当前回答

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

解决方法一:编辑。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

其他回答

要定位ini文件,首先运行

php -i | grep -i "loaded configuration file"

然后打开文件并更改

upload_max_filesize = 2M
post_max_size = 2M

将2M替换为你想要的大小,例如100M。

我有一篇博客文章,有更多的信息http://www.seanbehan.com/how-to-increase-or-change-the-file-upload-size-in-the-php-ini-file-for-wordpress

我知道这个问题有很多答案,但在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

我过去也遇到过同样的问题。我通过。htaccess文件修复了它

当你通过。htaccess修改php配置时,你应该把配置放进去 IfModule标签,否则内部服务器错误将产生。

这是一个例子,对我来说很管用:

<IfModule mod_php5.c>
   php_value upload_max_filesize 40M
   php_value post_max_size 40M
</IfModule>

如果你想了解更多,这是php参考资料。 http://php.net/manual/en/configuration.changes.php

很多次我都注意到共享主机的站点不允许更改php.ini文件中的设置。甚至根本不能创建.htaaccess文件。在这种情况下,人们可以尝试以下事情

ini_set('upload_max_filesize', '10M');
ini_set('post_max_size', '10M');
ini_set('max_input_time', 300);
ini_set('max_execution_time', 300);

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

解决方法一:编辑。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