Reputation: 327
I have Laravel 5.3 with local filesystem. The APP_URL in my .env is:
http://localhost/myapp
I linked the storage to public
php artisan storage:link
I have an image in:
myapp/storage/app/public/defaults/test/file.png
In the database the path is saved like this:
public/defaults/test/file.png
So in my blade.php I try to access the image
<img src="{{ Storage::url($user->test)}}" width="" alt="">
But the image isn't showing and the URL of the image is:
http://localhost/storage/defaults/test/file.png
so why it's ignoring the app_url? and even if i change the link to this:
http://localhost/myapp/storage/defaults/test/file.png
the image isn't showing up.
How can I access the Image in my myapp/storage/app/public/defaults/test/file.png with Storage::url?
BR
Upvotes: 2
Views: 7292
Reputation: 4114
Try this:
1) Fire php artisan storage:link
command, if you haven't already
2) In database, store path relative to the root/public directory with prefix storage.
So if you have image at root/public/defaults/test/file.png, then in the database, store this path: storage/defaults/test/file.png
3) And finally in the view, display it like:
<img src="{{ asset($user->test )}}" width="" alt="">
Make sure $user->test has value storage/defaults/test/file.png
OR
3) If you don't want to store storage prefix into the database, then store path like:
defaults/test/file.png
and in view:
<img src="{{ asset('storage/' . $user->test )}}" width="" alt="">
Note: Tested with Laravel 5.4, but pretty sure that it will work for Laravel 5.3 as well.
Here is a complete demo that I have created which illustrates image uploading and displaying:
https://github.com/xparthx/laravel5.4-image-upload
Upvotes: 2
Reputation: 513
if you want to access file in your storage folder you need to create route for that link
Route::get('storage/defaults/test/{filename}', function ($filename)
{
$imagePath = storage_path() . '/defaults/test/' . $filename;
if(!File::exists($imagePath)) {
//Not have file do something here
};
$file = File::get($imagePath);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
});
now you can access by your link
You have another way such as keep your image in [your project]/public/images/ in this way you can access your image by http://localhost/images/file.png (no need to create route)
But I suggest your create a new folder in you server example /data/images and set vhost in apache for allow the path
Alias /images /var/www/data/images
<Directory /var/www/data/images>
IndexOptions FancyIndexing FoldersFirst
Options MultiViews Indexes
AllowOverride None
Order allow,deny
Allow from all
</Directory>
Hope this help
Upvotes: 3