简体   繁体   English

尽管php.ini中设置了10M,但无法上传大于2MB的图像

[英]Can't upload images larger than 2MB despite the 10M settings in php.ini

I'm using the next PHP code to upload images on my website: 我正在使用下一个PHP代码在我的网站上上传图片:

<?php
if(isset($_POST['submit_form'])) {
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 1048576*10)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
}
    ?>

It work completely fine as long as I upload files of up to 2MB size. 只要我上传最大2MB的文件,它就可以正常工作。 The limit I defined in the script is 10MB (1048576*10). 我在脚本中定义的限制为10MB(1048576 * 10)。

In my php.ini file (BlueHost) 10M is defined for "post_max_size" and "upload_max_filesize". 在我的php.ini文件(BlueHost)中,为“ post_max_size”和“ upload_max_filesize”定义了10M。

I also tried "ini_set()", but nothing changed. 我也尝试过“ ini_set()”,但没有任何改变。

When I try to upload a file that is larger than 2MB, it keeps "uploading" it non-stop. 当我尝试上传大于2MB的文件时,它会不停地“上传”它。 It never finishes, but seems that it tries (the loading icons of the browser is working). 它永远不会完成,但是似乎可以尝试(浏览器的加载图标正在运行)。

I tried to contact my hosting company, and they say the limit is 10MB and not 2. The script is from w3schools: http://www.w3schools.com/php/php_file_upload.asp 我试图与托管公司联系,他们说限制是10MB而不是2MB。脚本来自w3schools: http : //www.w3schools.com/php/php_file_upload.asp

I don't understand what I'm doing wrong. 我不明白我在做什么错。 I'm trying to figure this thing out for a while, but I really need your help now. 我想花点时间解决这个问题,但是现在我真的需要您的帮助。

  • phpinfo() shows both as 10M, nothing 2M. phpinfo()都显示为10M,没有显示为2M。
  • no .htaccess files with anything related to the file size limit. 没有与文件大小限制有关的任何.htaccess文件。
  • PHP Version 5.4.27 PHP版本5.4.27

Thank you in advance. 先感谢您。

  1. Have you looked at a phpinfo() readout to verify whether or not your changes to php.ini are actually having any effect? 您是否查看了phpinfo()读数以验证对php.ini所做的更改实际上是否有效?
  2. Did you restart your webserver every time you modified php.ini? 您是否每次修改php.ini时都重新启动Web服务器?
  3. Refer to these docs over at php.net 在php.net上参考这些文档

http://www.php.net/manual/en/ini.core.php#ini.max-file-uploads http://www.php.net/manual/zh/ini.core.php#ini.max-file-uploads

http://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes http://www.php.net/manual/zh/faq.using.php#faq.using.shorthandbytes

In certain cases file upload limit size in php is limited by memory_limit setting also. 在某些情况下,php中的文件上传限制大小也受memory_limit设置限制。 Perhaps raising this up to 16Mb or more would help? 也许将其提高到16Mb或更多会有所帮助? If it is not that high already.. 如果不是那么高..

There's a few things to consider here. 这里有几件事要考虑。 First start with ied3vil's suggestions, then come back here if you're still not running. 首先从ied3vil的建议开始,然后如果您仍然没有运行,请回到这里。

I'm going to assume you're using apache as it's the common configuration for PHP. 我将假设您正在使用apache,因为它是PHP的常用配置。 You need to determine what mode PHP is running in, and ensure you are editing the correct php.ini file. 您需要确定PHP在哪种模式下运行,并确保您在编辑正确的php.ini文件。 There are very often 2 or more php.ini files on any given server, or even on any given context root. 在任何给定的服务器上,甚至在任何给定的上下文根上,通常都有2个或更多php.ini文件。 If PHP configuraiton has been set to allow recursive overrides, it's possible you have a php.ini file anywhere along your path to the file being served or bootstrapped. 如果PHP配置已设置为允许递归覆盖,则可能在您要提供或引导的文件的路径上的任何位置都有php.ini文件。

Secondly, PHP is not the only component of your architecture that can constrain file upload sizes. 其次,PHP并不是体系结构中唯一可以限制文件上传大小的组件。 Apache can set limits to the HTTP Request Body size using the LimitRequestBody directive, which can also be set to allow recursive overrides in a number of configurations... between allowing overrides inside of a <Directory /path/to/dir> directive, or even allowing it to be set in an .htaccess file. Apache可以使用LimitRequestBody指令设置对HTTP请求LimitRequestBody大小的限制,也可以将其设置为允许在多种配置中进行递归覆盖...之间允许在<Directory /path/to/dir>指令内部进行覆盖,或者甚至允许将其设置为.htaccess文件。

Things to check if you're still not working after reviewing ied3vil's answer: 查看ied3vil的答案后检查您是否仍无法工作的事情:

  • is the relevant master php.ini file being edited (the apache php.ini file or the php5 config php.ini file. This will depend on if you're running PHP in cgi mode or not 是正在编辑的相关主php.ini文件(apache php.ini文件还是php5 config php.ini文件。这取决于您是否以cgi模式运行PHP
  • does apache have any settings specifying limits to the request size allowed apache是​​否有任何设置指定对允许的请求大小的限制
  • if either apache or php is configured to allow overriding of configuration via things like .htaccess , directory specific php.ini or .conf files with vhost definitions, you will want to look for those along the path for the file upload settings previously mentioned. 如果将apache或php配置为允许通过诸如.htaccess ,具有vhost定义的特定于目录的php.ini.conf文件之类的配置来覆盖配置,则您将需要沿路径查找那些用于上述文件上传设置的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM