Boris56's avatar

Show image on storage

Hello,

After research, I can not find good way to show the images that are upload in the "storage" folder.

How to get the right path, for example: "ndd.com/image-patch-on-storage" files to the "storage" folder and show this on view?

Thank you.

0 likes
6 replies
SaeedPrez's avatar

The storage folder is outside the public folder so you can't access the files via ndd.com/path/my_image.jpg.

You need something like this..

return response()->file(Storage::get('my_image.jpg'), ['Content-Type' => 'image/jpeg']);
TheNodi's avatar

Hi,

I would suggest you to make a symbolic link from the upload directory (storage) to the public directory. Something like:

/public/image-patch-sur-storage => /storage/app/images/

Boris56's avatar

Why we have a public folder on storage if we can't access easy to files ?

SaeedPrez's avatar

@Boris56 It's not hard to access the storage files, you got a solution from me and another solution from @TheNodi and both take little effort to implement.

The way storage is setup, it gives you a lot more control because you can store files on other servers, in the cloud, and users have to access the files through your app, which allows you to protect your files and add functions (for example count number of times a file is accessed) and a lot more compared to accessing files directly.

You don't have to use Storage if you don't want/like to, you could use standard PHP or find another package.

Boris56's avatar
Boris56
OP
Best Answer
Level 2

Finaly, I found simple solution, a route with a controller:

The route:

Route::group([
    'prefix' => 'medias'
], function () {
    Route::get('/{name}', 'MediaController@index');
});

The controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use Response;
use Storage;

class MediaController extends Controller
{
    /**
     * @param $name
     * @return \Illuminate\Http\Response
     */
    public function index($name)
    {
        if (!Storage::exists($name)) {
            return Response::make('File no found.', 404);
        }

        $file = Storage::get($name);
        $type = Storage::mimeType($name);
        $response = Response::make($file, 200)->header("Content-Type", $type);

        return $response;
    }
}
5 likes
xcim's avatar

@Boris56 your solutions is very clean, can you please give me an example how to use your code to show an image in the view blade?

Thanks

Please or to participate in this conversation.