Skip to content

Instantly share code, notes, and snippets.

@edwinheij
Last active August 20, 2022 22:17
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save edwinheij/f6f246c258f390ae41e637194b1169f3 to your computer and use it in GitHub Desktop.
Save edwinheij/f6f246c258f390ae41e637194b1169f3 to your computer and use it in GitHub Desktop.
Stream large file with Laravel
<?php
//disable execution time limit when downloading a big file.
set_time_limit(0);
/** @var \League\Flysystem\Filesystem $fs */
$fs = Storage::disk('local')->getDriver();
$fileName = 'bigfile';
$metaData = $fs->getMetadata($fileName);
$stream = $fs->readStream($fileName);
if (ob_get_level()) ob_end_clean();
return response()->stream(
function () use ($stream) {
fpassthru($stream);
},
200,
[
'Content-Type' => $metaData['type'],
'Content-disposition' => 'attachment; filename="' . $metaData['path'] . '"',
]);
@khanzadimahdi
Copy link

using this logic can cause heavy traffic on server and ram usage.

@formidablefrank
Copy link

Hi! Can you suggest another method that conserves ram usage?

@mirko77
Copy link

mirko77 commented Nov 28, 2019

XSend

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment