Yann Btd
Yann Btd

Reputation: 419

Accessing an uploaded file on frontend with Laravel

I upload and store picture. Into my blade.php I have this line:

<img src="{{ storage_path('app\\'. $image) }}" />

this line generate this line:

<img src="C:\laragon\www\ProjectName\storage\app\uploads/images/IQeJo4w9lKqSxufTySpYKv1m9Z0TOV3PIYIvlN3g.jpeg" />

The image doesn't appears, but if I copy/past the C:\ link into my browser urlfield, I have the image visible.

Have you any idea to show the image with the img tag?

Upvotes: 1

Views: 6152

Answers (4)

Yann Btd
Yann Btd

Reputation: 419

@Travis: I have this:

'url' => env('APP_URL', 'http://localhost'),

'asset_url' => env('ASSET_URL', null),

Upvotes: 0

Travis Britz
Travis Britz

Reputation: 5552

You stored your image inside /storage/app/uploads, which is not a public directory. The public directory is /storage/app/public.

To save an image to the public disk instead, try this in a file upload controller:

$path = $request->file('image')->storePublicly('uploads/images');

(Save $path to the database)

Then inside your blade view:

<img src="{{ Storage::disk('public')->url($path) }}">

For this to work, you also need to link the public storage disk if you have not already:

php artisan storage:link

storage_path() gives you the internal path to the file, which will not work for creating links to an image that can be served by a webserver.

Upvotes: 2

Prafulla Kumar Sahu
Prafulla Kumar Sahu

Reputation: 9693

If you have created symlink you can access the file with

$path = asset('storage/' . $image);

try and let me know.

Upvotes: 2

Jerodev
Jerodev

Reputation: 33186

storage_path will give you the absolute path to the file on your local file system. You should never use this as a public URL. It won't work either. The reason you can see this image is because the web server is probably running on your own system.

Files in the storage folder are outside of the hosted part of the website and because of this not directly accessible.

You should take a look at the public disk if you want your files to be accessible directly.

Upvotes: 1

Related Questions