Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well I feel silly. This turned out to be just another PHP-ism. Apparently even though I was flushing the output buffer with <code>ob_flush</code> which (I thought) should have been sending the headings and chunks to the browser, the headers and output weren't actually getting flushed to the browser until the script finished.</p> <p>Even though the output is self was getting flushed, you still have to explicitly <code>flush</code> the write buffers of PHP and the web server back to the client. Not doing this lead to the memory expansion, and the download prompt not showing until the entire download completed.</p> <p>Here is a version of the working method:</p> <pre><code>public function download() { $file_info ... //Assume init'ed from WS or DB //Allow for long running process set_time_limit(0); //File Info $filesize = $file_info-&gt;get_filesize(); $fileid = $file_info-&gt;get_id(); $filename = $file_info-&gt;get_name(); $offset = 0; $chunksize = (1024 * 1024); //Clear any previous data ob_clean(); ob_start(); //Output headers to notify browser it's a download header('Content-Type: application/octet-stream'); header('Content-Length: ' . $filesize); header('Content-Disposition: attachment; filename="' . $filename . '"'); while($offset &lt; $filesize) { //Retrieve chunk from service $chunk = $this-&gt;dl_service()-&gt;download_chunked_file($fileid, $offset, $chunksize); if($chunk) { //Immediately echo out the stream $chunk-&gt;render(); //NOTE: The order of flushing IS IMPORTANT //Flush the data to the output buffer ob_flush(); //Flush the write buffer directly to the browser flush(); //Cleanup and prepare next request $offset += $chunksize; unset($chunk); } } //Exit the script immediately to prevent other output from corrupting the file exit(0); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload