Last active
August 20, 2022 22:17
-
-
Save edwinheij/f6f246c258f390ae41e637194b1169f3 to your computer and use it in GitHub Desktop.
Stream large file with Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'] . '"', | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi! Can you suggest another method that conserves ram usage?