简体   繁体   中英

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
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. The limit I defined in the script is 10MB (1048576*10).

In my php.ini file (BlueHost) 10M is defined for "post_max_size" and "upload_max_filesize".

I also tried "ini_set()", but nothing changed.

When I try to upload a file that is larger than 2MB, it keeps "uploading" it non-stop. 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

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.
  • no .htaccess files with anything related to the file size limit.
  • PHP Version 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?
  2. Did you restart your webserver every time you modified php.ini?
  3. Refer to these docs over at php.net

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

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

In certain cases file upload limit size in php is limited by memory_limit setting also. Perhaps raising this up to 16Mb or more would help? 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.

I'm going to assume you're using apache as it's the common configuration for PHP. You need to determine what mode PHP is running in, and ensure you are editing the correct php.ini file. There are very often 2 or more php.ini files on any given server, or even on any given context root. 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.

Secondly, PHP is not the only component of your architecture that can constrain file upload sizes. 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.

Things to check if you're still not working after reviewing ied3vil's answer:

  • 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
  • does apache have any settings specifying limits to the request size allowed
  • 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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