3

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);
7
  • 1
    run php_info() after the ini_set. Usually upload_max_filesize can't be set with ini_set Commented Apr 22, 2012 at 14:28
  • You're right, it wasn't set with ini_set. However, it is set at 100M by the host, that should be enough for a 99MB file, right? Also, the max_input_time is set at 60, isn't that to low either? Commented Apr 22, 2012 at 14:40
  • I would say that uploading 99MB file with 100MB limit is to risky - you should take ~10% extra for headers, control packets and etc. 100MB in 60sec is 1.7mb/s upload speed (13.5Mb upload link). probably it won't be enough Commented Apr 22, 2012 at 14:46
  • My host just increased the upload_max_filesize to 1024 and max_input_time to 3600 and it is still not working. This is very strange :\ Commented Apr 22, 2012 at 14:58
  • check in the apache error_log file Commented Apr 22, 2012 at 15:03

1 Answer 1

3

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";
  }
?> 
Sign up to request clarification or add additional context in comments.

4 Comments

Ok, I've checked everything and it still fails. All config variables are set to huge values, the error_log file is clean and the access_log always shows OK 200. I'm lost here :\
could you upload the last rows of the access log. The last parameter should be the response size. as well you can put few "echo" command and see how far the scrpit gets. BTW, what happens if you upload small file? Try the script I've added.
Can I use that script if I'm getting the file within the php://input?
I don't think you'll need to user php input stream for this. anyway is a good way to try to breakdown the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.