Laravel 5 Flysystem - download file from remote disk

筅森魡賤 提交于 2019-11-30 09:17:24
Frank de Jonge

Frank here from Flysystem.

The preferred way to do this would be to use the readStream output in combination with Response::stream.

<?php

$fs = Storage::disk('diskname')->getDriver();
$stream = $fs->readStream($file);

return Response::stream(function() use($stream) {
    fpassthru($stream);
}, 200, [
    "Content-Type" => $fs->getMimetype($file),
    "Content-Length" => $fs->getSize($file),
    "Content-disposition" => "attachment; filename=\"" . basename($file) . "\"",
]);

The $fs is the League\Flysystem\Filesystem instance. I believe there is a method to retrieve this instance in the filesystem class Laravel provides.

Is adding Storage::disk() sufficient for making Laravel look in this location rather than locally?

No, that wouldn't affect response()->download() calls.

Something like this should work:

return response()->download(Storage::disk('rackspace')->get('file-library/' . $file->filename));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!