While setting up an online file management system, and now I have hit a block.

I am trying to push the file to the client using this modified version of readfile:

function readfile_chunked($filename,$retbytes=true) { 
   $chunksize = 1*(1024*1024); // how many bytes per chunk 
   $buffer = ''; 
   $cnt =0; 
   // $handle = fopen($filename, 'rb'); 
   $handle = fopen($filename, 'rb'); 
   if ($handle === false) { 
       return false; 
   while (!feof($handle)) { 
       $buffer = fread($handle, $chunksize); 
       echo $buffer; 
       if ($retbytes) { 
           $cnt += strlen($buffer); 
       $status = fclose($handle); 
   if ($retbytes && $status) { 
       return $cnt; // return num. bytes delivered like readfile() does. 
   return $status; 

But when I try to download a 13 MB file, it's just breaking at 4 MB. What would be the issue here? It's definitely not the time limit of any kind because I am working on a local network and speed is not an issue.

The memory limit in PHP is set to 300 MB.

Thank you for any help.


There's almost certainly something in the error log, assuming you've got it turned on. Check that and amend the question.

Written by timdev

Nope. Nothing relating to this in the error log. All I have is some deprecation warnings from different scripts.

Written by Nirmal

What client are you using to dopwnload the content? Does the behaviour change if you use a different client? My Guess would be that the agent you are using can't cope with large chunked encoding. Try a different client / and /or sniffing the traffic.

Written by symcbean

@symcbean - I tried using both IE and Chrome, and both are responding the same way. If they can't handle the chunked encoding, they shouldn't be taking the first 4 MB, I believe. And, how to sniff the traffic? Haven't done that before. Thanks!

Written by Nirmal

an excellent packet sniffer is Wireshark. Check to see whether data is still streaming after the error, and check to see whether any error messages are being sent back that for some reason you're not seeing.

Written by eykanal

Accepted Answer

Most likely you are hitting the response buffer limit set by your webserver. IIS and FastCGI are known to have 4mb as the default buffer size. I would start your search with looking into the webserver<->PHP SAPI configuration.

Written by m1tk4
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki