5

I have store the file in storage/app/files folder by $path=$request->file->store('files') and save the path "files/LKaOlKhE5uITzAbRj5PkkNunWldmUTm3tOWPfLxO.doc" it in a table's column name file.

I have also linked storage folder to public through php artisan storage:link.

In my view blade file, I put this

<a href="@if(count($personal_information)) {{asset('storage/'.$personal_information->file)}} @endif" download>Download File</a>

and the link for download file is http://localhost:8000/storage/files/LKaOlKhE5uITzAbRj5PkkNunWldmUTm3tOWPfLxO.doc

But I get the error

NotFoundHttpException in RouteCollection.php line 161

If I add /app after the /storage it gives the same error. How can I download file from my storage/app/files folder?

Townim Faisal
  • 325
  • 1
  • 4
  • 11
  • refer to [Link](http://stackoverflow.com/questions/31648243/laravel-5-notfoundhttpexception-in-routecollection-php-line-143) – Nadeem_MK Apr 10 '17 at 06:26
  • This will help you......... [here](https://stackoverflow.com/questions/43315857/cannot-download-file-from-storage-folder-in-laravel-5-4) – Abdulloh Olimov Feb 10 '23 at 09:43

5 Answers5

15

Problem is storage folder is not publicly accessible in default. Storage folder is most likely forsave some private files such as users pictures which is not accessible by other users. If you move them to public folder files will be accessible for everyone. I had similar issue with Laravel 5.4 and I did a small go around by writing a route to download files.

Route::get('files/{file_name}', function($file_name = null)
{
    $path = storage_path().'/'.'app'.'/files/'.$file_name;
    if (file_exists($path)) {
        return Response::download($path);
    }
});

Or you can save your files into public folder up to you.

Anar Bayramov
  • 11,158
  • 5
  • 44
  • 64
2

I'm using Laravel 6.X and was having a similar issue. The go around according to your issue is as follows: 1)In your routes/web.php do something like

/**sample route**/
Route::get('/download/{path}/', 'MyController@get_file');

2)Then in your controller (MyController.php) for our case it should look like this:


    <?php
    
    namespace App\Http\Controllers;
    
    use App\Http\Controllers\Controller;
    
    class MyController extends Controller
    {
        public function get_file($path)
        {
            /**this will force download your file**/
            return response()->download($path);
        }
    }

    
1

If you have your files is stored in the public directory then use: public_path(). But if your files are stored in the storage directory, then use: storage_path()

NOTE: If your storage folder contains only attachment folder then consider having it this way: storage_path('photos/');

$destination = storage_path('storage/photos/'); 
               or
$destination = public_path('storage/photos/');
$filename = "user_3423423465.png";
$pathToFile = $destination.$filename;
return response()->download($pathToFile,'user_profile_photo.png');
Ekfinbarr
  • 41
  • 3
-1

Laravel8

Create a link

<a href="{{ url('download?path='. $user->avatar) }}">
    Download
</a>

Define a route

Route::get('download', [DownloadController::class,'download']);

Handle it

public function download(Request $request){
    return Storage::download($request->path);
}
Daud khan
  • 2,413
  • 2
  • 14
  • 18
-2

For Laravel 8

Upload File to Storage Folder

in this example, i have created a test folder inside storage folder name: uploadedfiles

In FileSystem.php

'uploadedfiles' => [
   'driver' => 'local',
   'root' => storage_path('uploadedfiles'),
],

Upload File

public function upload_file()
{
  $file_name = "files_"."_".strtotime("now")."_".$_FILES['file']['name'];
  $content = file_get_contents($_FILES['file']['tmp_name']);
  Storage::disk('uploadedfiles')->put($file_name,$content);
}

Download File from Storage Folder

Route::get('download/files/{filename}', function($filename) {
  $file = Storage::disk('uploadedfiles')->download($filename);
  return $file;
});
Yuvraj Hinger
  • 198
  • 1
  • 6