Search code examples
phplaravellaravel-5laravel-5.7laravel-storage

Cannot Access Laravel Storage Directory


I'm using Laravel 5.7. On my localhost, my storage works fine, and all images show as well, but on my host, new images don't display.

I use below code inisde the blade.php file:

{{ asset("storage/$slider") }}

On my localhost it shows images:

localhost:8000/storage/sliders/HKeGwcvnXbjuiA6g9wsjnoqphJc5DGup78D92b4F.jpeg

On my website I cannot access:

http://mywebsite.com/storage/sliders/HKeGwcvnXbjuiA6g9wsjnoqphJc5DGup78D92b4F.jpeg


Solution

  • now I use this route and It worked:

    Route::get('storage/sliders/{filename}', function ($filename)
    {
    
        $path = storage_path('app/public/sliders/' . $filename);
    
        if (!File::exists($path)) {
            abort(404);
        }
    
        $file = File::get($path);
        $type = File::mimeType($path);
    
        $response = Response::make($file, 200);
        $response->header("Content-Type", $type);
    
        return $response;
    });
    

    if you have another sub-directories you can use:

    Route::get('storage/{path}/{filename}', function ($path,$filename)
    {
    
        $path = storage_path('app/public/'.$path.'/' . $filename);
    
        if (!File::exists($path)) {
            abort(404);
        }
    
        $file = File::get($path);
        $type = File::mimeType($path);
    
        $response = Response::make($file, 200);
        $response->header("Content-Type", $type);
    
        return $response;
    });