Jump to content

readfile download stopping halfway through


Octo

Recommended Posts

I wrote a small application to force downloads of various large video files using readfile. This worked fine. However, the users have changed hosts and now it doesn't work. Small files are fine, but large files will cut off before finishing. Test file is 114MB, but it is always cut off at exactly 67.2MB or 51.4MB depending on a couple criteria.

 

I've stripped the application down to the key part that's not working and run it on a test page the just immediately goes the file - no logins or any of that shenanigans and htaccess blocking either:

 

$file = some/file/on/the/server.wmv; //114MB

hea der('Content-Description: File Transfer');

hea der("Content-Type: application/octet-stream");

hea der("Content-Disposition: attachment; filename=" .basename($file));

hea der("Content-Transfer-Encoding: binary");

hea der('Expires: 0');

hea der('Cache-Control: must-revalidate, post-check=0, pre-check=0');

hea der('Pragma: public');

hea der('Accept-Ranges: bytes');

hea der("Content-Length: ".filesize($file));

ob_clean();

flush();

@readfile($file);

 

(gaps in the header words are to prevent forum breakage)

 

I've tried the following variations:

  • transfer encoding - chunked (this results in getting a 51.4 MB download rather than 67.2)
  • content-type application/force-download
  • tried the default force-download code from the php.net manual
  • tried the chunking function variations on php.net readfile page
  • tried without accept ranges originally (only spotted that on this site)
  • set_time_limt to one hour
  • set max memory to 300MB (phpinfo has shown these changes did get accepted)
  • used htacess to disable gzip and deflate (this results in getting a 51.4 MB download rather than 67.2)
    (SetEnv no-gzip dont-vary
    or
    RewriteEngine On
    RewriteRule . - [E=no-gzip:1]
    or
    RemoveOutputFilter DEFLATE html txt xml css js php wmv)

 

None of these have solved the issue. The code worked fine on the old site, works fine on my site, and works fine on my test server. Direct downloads from the broken site also work fine, just not readfile downloads. The filesize is reading correctly if I echo just that, and it reads properly on the progress bar when downloading through readfile.

 

The hosting company are blaming my coding. The htaccess is the bit I'm least sure of and I find it hard to get solid info on this part on the web.

 

Is there anything wrong with my code that could be causing this? Am I missing something? What could be causing the problem?

 

Many Thanks

Link to comment
Share on other sites

Thanks for the reply

 

Not had any errors display in any situation mentioned.

 

Have tried fread in both a custom chunked function (from php.net page), and now just tried it non-chunked. Both downloads using this method halt at 51.4MB.

 

Another thing: Have turned output-buffering off using ini_set (can you tell I'm clutching at straws here? :) )

Link to comment
Share on other sites

The code you did post has an @ in it. That would prevent ANY error_reporting/logging.

 

You should have error_reporting set to E_ALL, log_errors set to ON, error_log set to a known (and tested) location, and NO/NONE/ZERO @'s in your code.

 

Any chance that the test file is corrupted and cannot be read past a point? Did you upload it to the server using FTP in binary mode? Try downloading the test file using FTP and see if it is intact.

 

Link to comment
Share on other sites

Test file is not corrupt (if only!). As mentioned, download via the url directly works fine and same test file works fine on other sites.

 

Regarding error reporting - the @ wasn't in there until recently (I know what it's for) just forgot I put it on there. Will try things again without it and with all error reporting on, but didn't get any error reports before it sneaked it's way in there.

Link to comment
Share on other sites

Apologies for the double post, just thought I'd post the solution:

 

Turns out the hosting - after blaming it on my coding - had the server set up wrong so that it wasn't reading the php.ini properly, it was being overridden by another one even though phpinfo showed my changes propogating. This meant that when I tried to set up error logging as suggested I just couldn't get it to work for some reason. Exact same logging code worked fine on other servers and created a log file, just not this one.

 

So the PHP ini was limiting it to two minutes, but when we fixed that it then timed out at five minutes. This turned out to be the IIS server which has a CGITimeout counter in the Metabase.xml set to five minutes. So I changed that. The company's hosting package didn't really come with any real support, so we were really left on our own. Thank god one of the support guys was nice enough look into and spotted it was set up wrong (think it php twice in the MIME types too) otherwise the client would've been screwed and kinda not their fault.

 

Anyway, thanks for the replies.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.