Cannot download file from storage folder in laravel 5.4

前端 未结 2 2019
误落风尘
误落风尘 2020-12-11 09:18

I have store the file in storage/app/files folder by $path=$request->file->store(\'files\') and save the path \"files/LKaOlKhE5uITzAbRj5PkkNunWldmUTm3tOWP

相关标签:
2条回答
  • 2020-12-11 09:41

    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.

    0 讨论(0)
  • 2020-12-11 09:53

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题