Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php://input fails with large files

I'm using a upload script with ajax and PHP and it works wonders for files smaller than 80MB. However, if the file is bigger than 80MB it fails, it doesn't even output anything at all.

The code is:

$maxsize = getMaxFileSize();
$finalfile = $uploadpath . $finalname;
$putdata = fopen("php://input", "r");
$fp = fopen($finalfile, "w");
$filesizecalc = 0;
while ($data = fread($putdata, 1024)) {
    fwrite($fp, $data);
    $filesizecalc = $filesizecalc + 1024;
}

fclose($fp);
fclose($putdata);
if ($filesizecalc <= $maxsize) {
    addFile($_SESSION['userdata']['userid'], $finalname);
    echo "$fn uploaded";
} else {
    unlink($finalfile);
}
exit();

This works fine with almost all files < 80 MB, but for files bigger than 80MB it doesn't output a thing, so I don't even know what's going wrong, even though I set

error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', 1);
ini_set('memory_limit', '1024M');
ini_set('upload_max_filesize', '1024M');
ini_set('post_max_size', '1024M');
ini_set('max_input_time', 10000);
ini_set('max_execution_time', 10000);
like image 876
Rui Gomes Avatar asked Dec 05 '25 02:12

Rui Gomes


1 Answers

Lets write it down as solution so it can be read correctly rather than digging in the comments.

  • Check your php_info() after the ini_set commands, some config variables cannot be changed from the script there are ~ 6 different values controlling big uploads. Check ALL of them (the list and the explemenations can be found here )
  • Check in the apache error_log file for the real error. (or check the access_log to see what was the request status returned by the server)
  • there may be some Application firewall or apache config that limits the request time. In this case you'll see response code such as "connection reset".

Try the W3Scools upload script:

<?php
if (true)
  {
  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";
  }
?> 
like image 137
SimSimY Avatar answered Dec 06 '25 16:12

SimSimY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!