php 更改最大上传文件大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2184513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change the maximum upload file size
提问by Yo Momma
I have a website hosted on a PC I have no access to. I have an upload form allowing people to upload mp3 files up to 30MB big. My server side script is done in PHP.
我有一个网站托管在我无法访问的 PC 上。我有一个上传表单,允许人们上传最大 30MB 的 mp3 文件。我的服务器端脚本是用 PHP 完成的。
Every time I try and upload a file, I receive an error claiming that the file exceeds the maximum size allowed, so I need to increase the size. My research on the web suggested changing the .htaccessfile which I do not have access to, so that won't work. Others suggested that I should add a custom php.inifile to my root which did not work. Any other suggestions?
每次我尝试上传文件时,都会收到一条错误消息,声称该文件超出了允许的最大大小,因此我需要增加大小。我在网上的研究建议更改.htaccess我无权访问的文件,这样就行不通了。其他人建议我应该php.ini向我的根目录添加一个自定义文件,但它不起作用。还有其他建议吗?
回答by meagar
You need to set the value of upload_max_filesizeand post_max_sizein your php.ini :
您需要设置的值upload_max_filesize,并post_max_size在你的php.ini:
; Maximum allowed size for uploaded files.
upload_max_filesize = 40M
; Must be greater than or equal to upload_max_filesize
post_max_size = 40M
After modifying php.ini file(s), you need to restart your HTTP server to use new configuration.
修改 php.ini 文件后,您需要重新启动 HTTP 服务器才能使用新配置。
If you can't change your php.ini, you're out of luck. You cannot change these values at run-time; uploads of file larger than the value specified in php.ini will have failed by the time execution reaches your call to ini_set.
如果你不能改变你的 php.ini,那你就不走运了。您不能在运行时更改这些值;上传大于 php.ini 中指定值的文件将在执行到达您调用ini_set.
See the Description of core php.ini directives.
请参阅核心 php.ini 指令的说明。
回答by Andrew Moore
You can change it via an .htaccessfile.
您可以通过.htaccess文件更改它。
.htaccessfiles are stored in the same directory as your .phpfiles are. They modify configuration for that folder and all sub-folders. You simply use them by creating an .htaccessfile in the directory of your choice (or modify it if present).
.htaccess文件存储在与您的.php文件相同的目录中。他们修改该文件夹和所有子文件夹的配置。您只需.htaccess在您选择的目录中创建一个文件(或修改它,如果存在)来使用它们。
The following should enable you to increase your upload limit (if the server provider allows PHP config changes via .htaccess).
以下应该使您能够增加上传限制(如果服务器提供商允许通过 更改 PHP 配置.htaccess)。
php_value upload_max_filesize 40M
php_value post_max_size 42M
回答by user1641252
I had the same problem and i created a .user.ini file and put it in the directory in which the upload script was located. Than inside that file i set these these two values:
我遇到了同样的问题,我创建了一个 .user.ini 文件并将其放在上传脚本所在的目录中。比在该文件中我设置了这两个值:
upload_max_filesize = 40M
post_max_size = 40M
and it worked great for me!
它对我很有用!
回答by Manjar
You can also use ini_set function(only for PHP version below 5.3):
您也可以使用ini_set 函数(仅适用于 5.3 以下的 PHP 版本):
ini_set('post_max_size', '64M');
ini_set('upload_max_filesize', '64M');
Like @acme said, in php 5.3 and above this settings are PHP_INI_PERDIR directives so they can't be set using ini_set. You can use user.ini instead.
就像@acme 所说的那样,在 php 5.3 及以上版本中,此设置是 PHP_INI_PERDIR 指令,因此无法使用 ini_set 进行设置。您可以改用 user.ini。
回答by seanbehan
To locate the ini file, first run
要找到 ini 文件,首先运行
php -i | grep -i "loaded configuration file"
Then open the file and change
然后打开文件并更改
upload_max_filesize = 2M
post_max_size = 2M
replacing the 2M with the size you want, for instance 100M.
用您想要的大小替换 2M,例如 100M。
I've got a blog post about with a little more info too http://www.seanbehan.com/how-to-increase-or-change-the-file-upload-size-in-the-php-ini-file-for-wordpress
我也有一篇关于更多信息的博客文章http://www.seanbehan.com/how-to-increase-or-change-the-file-upload-size-in-the-php-ini-文件换wordpress
回答by Eyad Farra
I have the same problem in the past .. and i fixed it through .htaccessfile
我过去有同样的问题..我通过.htaccess文件修复了它
When you make change on php configration through .htaccessyou should put configrations in
IfModuletag, other that the Internal server errorwill arise.
当您通过 php 配置进行更改时,.htaccess您应该将配置放在
IfModule标签中,否则会出现内部服务器错误。
This is an example, it works fine for me:
这是一个例子,它对我来说很好:
<IfModule mod_php5.c>
php_value upload_max_filesize 40M
php_value post_max_size 40M
</IfModule>
And this is php referance if you want to understand more. http://php.net/manual/en/configuration.changes.php
如果您想了解更多,这是 php 参考。 http://php.net/manual/en/configuration.changes.php
回答by Pearce
I resolved this issue by creating a file called .user.iniin the directory where the PHP file scripts reside (this means any PHP script in this directory gets the new file size limit)
我通过.user.ini在 PHP 文件脚本所在的目录中创建一个名为的文件解决了这个问题(这意味着该目录中的任何 PHP 脚本都会获得新的文件大小限制)
The contents of .user.iniwere:
内容.user.ini是:
upload_max_filesize = 40M
post_max_size = 40M
回答by nrmb
I had the same problem. I have tried three ways that were usually suggested:
我有同样的问题。我尝试了三种通常被建议的方法:
- functions.php
- php.ini
- .htaccess
- 函数.php
- 配置文件
- .htaccess
none if which solved my problem. I am using godaddy and came across a suggested solution which was:
没有如果这解决了我的问题。我正在使用 Godaddy 并遇到了一个建议的解决方案,它是:
- got to Web Hosting, then Manage
- Under Softwareselect Select PHP version
- Select Switch to PHP Optionsfound on the top right corner of the table in font color: blue
- On the bottom most part, you'll probably have upload_max_filesize = 2M
- Now, feel free to change it
- Be sure to click the Savebutton!
- Now go to your wp-admin panel, select Mediathen Add
- 到虚拟主机,然后管理
- 在软件下选择选择 PHP 版本
- 选择位于表格右上角的“切换到 PHP 选项”,字体颜色为:蓝色
- 在最底部,你可能有upload_max_filesize = 2M
- 现在,随意更改它
- 一定要点击保存按钮!
- 现在转到您的 wp-admin 面板,选择媒体然后添加
Voila! Now you have a different max upload file size :)
瞧!现在你有一个不同的最大上传文件大小:)
回答by The EasyLearn Academy
many times i have noticed that site wit shared hosting do not allow to change settings in php.ini files. one also can not even crate .htaaccess file at all. in such situation one can try following things
很多次我注意到该站点机智共享主机不允许更改 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);
回答by Toskan
the answers are a bit incomplete, 3 things you have to do
答案有点不完整,你必须做的三件事
in php.ini of your php installation (note: depending if you want it for CLI, apache, or nginx, find the right php.ini to manipulate. For nginx it is usually located in /etc/php/7.1/fpmwhere 7.1 depends on your version. For apache usually /etc/php/7.1/apache2)
在你的 php 安装的 php.ini 中(注意:取决于你想要它用于 CLI、apache 还是 nginx,找到正确的 php.ini 来操作。对于 nginx,它通常位于/etc/php/7.1/fpm7.1 取决于你的版本的位置。对于 apache 通常/etc/php/7.1/apache2)
post_max_size=500M
upload_max_filesize=500M
memory_limit=900M
or set other values. Restart/reload apache if you have apache installed or php-fpmfor nginx if you use nginx.
或设置其他值。如果您安装了 apache,请重新启动/重新加载 apache ,如果您使用 nginx,请为 nginx安装php-fpm。

