Search code examples
phplaravellaravel-4download

Laravel Resumable Download


My PHP script is downloading a zip file to the user using Laravel's Response::download function :

$file    = public_path(). "/storage/rhcloud/lobby/0.1.zip";
$downloadName = "lobby-0.1";
$headers = array(
   'Content-Type: application/zip',
);
return Response::download($file, $downloadName, $headers);

The download works correctly. But, can't do the pause/resume download function.

I have searched all over the web, but didn't find anything. Does Laravel support this partial content downloading or should native PHP code should be used instead of Laravel's ?

Even if I add additional code, how can I make Response::download function to fetch data from the given bytes from user request header ?


Solution

  • If you don't specify the content-length (i.e. size of the file) the browser doesn't know how or where to resume the download from. Also, you can use basename() function to grab the file name from full path - no need to hard code it

    Try this:

    $file    = public_path(). "/storage/rhcloud/lobby/0.1.zip";
    $headers = [
       'Content-Type' => 'application/zip',
       'Content-Length' => filesize($file)
    ];
    return Response::download($file, basename($file), $headers);
    

    Or for older Laravel versions, like OP's, try:

    $file    = public_path(). "/storage/rhcloud/lobby/0.1.zip";
    $headers = array(
       'Content-Type: application/zip',
       'Content-Length: '. filesize($file)
    );
    return Response::download($file, basename($file), $headers);