Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot download file from storage folder in laravel 5.4

Tags:

laravel-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?

like image 904
Townim Faisal Avatar asked Dec 14 '22 23:12

Townim Faisal


2 Answers

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.

like image 63
Anar Bayramov Avatar answered Jun 05 '23 06:06

Anar Bayramov


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);
        }
    }

    
like image 44
Software Developer Avatar answered Jun 05 '23 07:06

Software Developer