marco987
marco987

Reputation: 97

Laravel 7. How to download a file from server (FTP)?

I created a disk in config/filesystems.php file

'ftp' => [
    'driver' => 'ftp',
    'host' => 'ftp.domain.org',
    'username' => 'username',
    'password' => 'password',
    'passive' => true,
    'timeout' => 30,
    'root' => '/',
    'url' => '/'
],

The connection is tested and works. On the server this file exists:

$file_path = "/folder/aaa.txt";

But I can't download it! In the controller I wrote:

$file = Storage::disk('ftp')->download($file_path);
return response()->download($file);

And this is the outcome:

Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException The file "HTTP/1.0 200 OK Cache-Control: no-cache, private Content-Disposition: attachment; filename=aaa.txt Content-Length: 0 Content-Type: text/plain Date: Fri, 10 Jul 2020 19:20:51 GMT" does not exist

Additional issue

Instead of downloading it, how do I display the same file in the browser? The following code does not work in this case:

return response()->file($file_path);

Upvotes: 1

Views: 8340

Answers (1)

apokryfos
apokryfos

Reputation: 40683

Both response()->download($file) and Storage::download($file) will create a download response so you only need one of the two. Since your file is in remote storage you can just keep:

return  Storage::disk('ftp')->download($file_path);

You can also customise the filename and headers. You can also (probably) make the file show inline by doing:

return  Storage::disk('ftp')->download($file_path, 'any.txt', [
   'Content-Disposition' => 'inline'
]);

Upvotes: 3

Related Questions