Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This happens because <code>Response::download()</code> loads the file in to memory before serving it to the user. Admittedly this is a flaw in the framework, but most people do not try to serve large files through the framework.</p> <p>Solution 1 - Put the files you want to download in the public folder, on a static domain, or cdn - bypass Laravel completely.</p> <p>Understandably, you might be trying to restrict access to your downloads by login, in which case you'll need to craft your own download method, something like this should work...</p> <pre><code>function sendFile($path, $name = null, array $headers = array()) { if (is_null($name)) $name = basename($path); // Prepare the headers $headers = array_merge(array( 'Content-Description' =&gt; 'File Transfer', 'Content-Type' =&gt; File::mime(File::extension($path)), 'Content-Transfer-Encoding' =&gt; 'binary', 'Expires' =&gt; 0, 'Cache-Control' =&gt; 'must-revalidate, post-check=0, pre-check=0', 'Pragma' =&gt; 'public', 'Content-Length' =&gt; File::size($path), ), $headers); $response = new Response('', 200, $headers); $response-&gt;header('Content-Disposition', $response-&gt;disposition($name)); // If there's a session we should save it now if (Config::get('session.driver') !== '') { Session::save(); } // Send the headers and the file ob_end_clean(); $response-&gt;send_headers(); if ($fp = fread($path, 'rb')) { while(!feof($fp) and (connection_status()==0)) { print(fread($fp, 8192)); flush(); } } // Finish off, like Laravel would Event::fire('laravel.done', array($response)); $response-&gt;foundation-&gt;finish(); exit; } </code></pre> <p>This function is a combination of <a href="https://github.com/laravel/laravel/blob/master/laravel/response.php#L183">Response::download()</a> and Laravel's <a href="https://github.com/laravel/laravel/blob/master/laravel/laravel.php#L193">shutdown process</a>. I've not had a chance to test it myself, I don't have Laravel 3 installed at work. Please let me know if it does the job for you.</p> <p><strong>PS:</strong> The only thing this script does not take care of is cookies. Unfortunately the <a href="https://github.com/laravel/laravel/blob/master/laravel/response.php#L300">Response::cookies()</a> function is protected. If this becomes a problem you can lift the code from the function and put it in your sendFile method.</p> <p><strong>PPS:</strong> There <em>might</em> be an issue with output buffering; if it is a problem have a look in the PHP manual at <a href="http://uk1.php.net/manual/en/function.readfile.php#example-2374">readfile() examples</a>, there's a method that should work there.</p> <p><em><strong>PPPS:</em></strong> Since you're working with binary files you might want to consider replacing <code>readfile()</code> with <a href="http://php.net/manual/en/function.fpassthru.php">fpassthru()</a></p> <p><strong>EDIT:</strong> Disregard PPS and PPPS, I've updated the code to use fread+print instead as this seems more stable.</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