Unable to retrieve the file_size for file on downloading storage file - Laravel

2.9k Views Asked by At

Just a simple question.

Trying to download the storage file but I'm unable to download the file from the storage folder.

Searched on google. Found two ways to download storage files. Tried both but in vain.

return response()->download(storage_path("app\public\uploads\1662531990_Dropshipping.docx"));
return Storage::disk('public')->download("app\public\uploads\1662531990_Dropshipping.docx", "1662531990_Dropshipping");

Yes, the above-given files exist inside the given path. Below is the screenshot of that directory and error screen.

It has something to do with the flysystem hence below are the related packages, I think, as a result of the composer show

league/config                      v1.1.1  Define configuration arrays with strict schemas an...   
league/flysystem                   3.2.1   File storage abstraction for PHP
league/flysystem-aws-s3-v3         3.0.0   AWS S3 filesystem adapter for Flysystem.
league/mime-type-detection         1.11.0  Mime-type detection for Flysystem

enter image description here

enter image description here

1

There are 1 best solutions below

0
Obergruppenfuhrer On

The easiest way to download the file from storage for Laravel 9 is using the download method as shown in the documentation.

return Storage::download('file.jpg');
 
return Storage::download('file.jpg', $name, $headers);

Below you will find a code example I used to download a pdf:

public function download(Publication $publication){
    //Path from the local storage directory (./storage/app/)
    $path = $publication->file_path;
    
    //Name of the file the user will see
    $slug = Str::slug($publication->title).'.pdf';

    $headers = [
        'Content-Type' => 'application/pdf',
     ];

     return Storage::download($path, $slug, $headers);
}

Hope this helps!