Note that there are some explanatory texts on larger screens.

plurals
  1. POStreaming Large Files from external Web Service through PHP
    text
    copied!<p>I am currently using external SOAP web services that allow chunked downloads/uploads of binary files (should allow larger files). I need to allow a end user to download files through the browser with a my PHP app. Serving small files works well, but 25MB+ files cause the web server to run out of memory.</p> <p>I am using the native PHP Soap Client (no MTOM support), and requesting the download by submitting a form. Currently it seems like the web server is trying to download the whole file before outputing anything to the browser (e.g. the "Download" prompt doesn't show until after the whole file has been process via PHP).</p> <p>My method looks something like this (sorry if it's messy, I've been hacking away at this problem for a while).</p> <pre><code>public function download() { $file_info_from_ws ... //Assume setup from $_REQUEST params //Don't know if these are needed gc_enable(); set_time_limit(0); @apache_setenv('no-gzip', 1); @ini_set('zlib.output_compression', 0); //File Info $filesize = $file_info_from_ws-&gt;get_filesize(); $fileid = $file_info_from_ws-&gt;get_id(); $filename = $file_info_from_ws-&gt;get_name(); $offset = 0; $chunksize = (1024 * 1024); //Clear any previous data ob_clean(); ob_start(); //Output headers header('Content-Type: application/octet-stream'); header('Content-Length: ' . $filesize); header('Content-Transfer-Encoding: binary'); header('Content-Disposition: attachment; filename="' . $filename . '"'); header('Accept-Ranges: bytes'); while($offset &lt; $filesize) { $chunk = $this-&gt;dl_service()-&gt;download_chunked_file($fileid, $offset, $chunksize); if($chunk) { //Immediately echo out the stream $chunk-&gt;render(); $offset += $chunksize; unset($chunk); //Shouldn't this trigger GC? ob_flush(); } } ob_end_flush(); } </code></pre> <p><strong>So my main question is:</strong> What is the best way to output large binary chunks from external resources (Webservices, DB, etc.) through PHP to the end user? Preferably without killing memory/CPU too much.</p> <p><strong>Also I'm curious about the following:</strong><br> Why wouldn't the Download prompt pop-up after the first output?<br> Why is memory not freed after each loop in the about method?</p>
 

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