8

I am storing files for a site on Rackspace using Flysystem. Uploading is no problem, having trouble figuring out how to start a download for a file - this is what I have tried

Storage::disk('rackspace');
return response()->download('file-library/' . $file->filename);

The result is that the file could not be found. Is adding Storage::disk() sufficient for making Laravel look in this location rather than locally? What is the best way to accomplish this?

NightMICU
  • 8,490
  • 25
  • 84
  • 114

2 Answers2

26

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.

Stepan Mazurov
  • 1,345
  • 10
  • 15
Frank de Jonge
  • 1,406
  • 12
  • 21
-4

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));
ceejayoz
  • 165,698
  • 38
  • 268
  • 341
  • `is_file() expects parameter 1 to be a valid path, string given`. Download's first parameter is a path (string) – NightMICU Mar 25 '15 at 21:43
  • @NightMICU You may have to pull it down into a temporary file, I guess. Or just link the user straight to Rackspace's storage. – ceejayoz Mar 25 '15 at 21:49
  • @ceeyaoz seems weird that they wouldn't have thought of this. I'd like to avoid having to mess with temporary files (the idea was to keep all files of this type off of my local file system) and was hoping to initiate a download without the user leaving the current page. – NightMICU Mar 25 '15 at 21:51
  • @NightMICU Whenever I've needed to do this sort of thing I've just redirected the user to a AWS S3 signed URL for the download. No reason to involve the web server at all in the download that way. More scalable, faster, and easier. – ceejayoz Mar 26 '15 at 13:44
  • @NightMICU I don't use Rackspace, but they appear to have similar systems in place. http://docs.rackspace.com/files/api/v1/cf-devguide/content/TempURL-d1a4450.html – ceejayoz Mar 26 '15 at 13:56