Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

900 MB http forced downloads rarely finish

I have tried all the headers. With and without content-length.

The problem is that the dowload craps out around halfway. But only most of the time.

Sometimes it works just fine.

Server resource usage is trivial. All config files are fine. I dont run php script while its going.

Has anyone seen this before? Is it not even my fault? Why sometimes?

$file = "http://domain.com/files/".$item_number.".mov";
header( 'Pragma: no-cache' );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Content-Description: File Download' );
header( 'Content-Type: application/force-download');
//header ('Content-Type: video/quicktime');//doesnt seem to download at all here
header( 'Content-Length: '.filesize( $file ) );
header( 'Content-Disposition: attachment; filename="'.basename( $file ).'"' );
header( 'Content-Transfer-Encoding: binary' );
readfile( $file ); 
exit();

Thank you, Sorry it's my first time.

like image 205
patinyo Avatar asked Jun 13 '12 00:06

patinyo


1 Answers

This can have several reasons.

You are saying that server resources are OK, but I think PHPs readfile() loads the entire file into memory before sending it. Fortunately there is a good solution in the docs you should definitely use to prevent running into problems with multiple downloads:)

Also, did you set your script execution time to infinite?

Next would be your server settings. Maybe they close the connection for some timeout/resource hog reason.

Use for instance Google Chromes developer tool to closely examine the headers you are sending. PHP sets some headers on its own. You should put session_cache_limiter(false); in the beginning of your script. If I remember correctly, even Expires: Thu, 19 Nov 1981 08:52:00 GMT... Why this is bad? Coming up next;)

You can send the Etag and Last-Modified header. I sum it is a unique identifier for your resource, that makes it possible to resume downloads. I think looking up good articles about this is better than just giving you some sentences here. In your case you could use PHPs stat() function to create an Etag.

There are some people struggling with headers and downloads, you could start (or fix your case) here

Summary:

  • Check your PHP/server expiry/limitations/resource settings
  • Be sure to send the file buffered
  • Make it possible to resume downloads
  • Check the headers you are sending

After this you should be fine. With resuming even if it should still break...

Good progress and success!

like image 70
nico gawenda Avatar answered Oct 20 '22 08:10

nico gawenda