Image Files Not Showing in Laravel App after Deploying on AWS Beanstalk - laravel

I have a Laravel Web Application deployed on AWS Beanstalk. Everything including the database works perfectly except for the image/file display. The logo, images, etc. aren't showing up. This is probably a file path problem. My images and files are stored in the "storage" directory. My path to the files are as such:
<img src="/storage/images/logo.png">
This works locally yet on the AWS server it doesn't. I tried all possible paths but still nothing worked. What am I doing wrong?

Try this one to retrieve the assets from the storage directory.
Storage::disk('local')->url('images/logo.png');
You can use it blade in the following format.
<img src="{{ Storage::disk('local')->url('images/logo.png') }}">
This will give the direct URL to /storage/images/logo.png

Also, you need to run the command:
$php artisan storage:link
Before running this command please check if the storage folder is already there or not inside the public folder if it's there then rename it to storage-bk and run the command.

Related

Laravel 6.2 on AWS elastic Beanstalk. Assets not loaded

I have hosted a laravel 6.2 app on AWS elastic Beanstalk. Previously everything was working fine when the site was static, but after I created an RDS instance and made the site dynamic, the site still works fine, but the javascripts and css file I have kept inside the public folder are not being loaded.
First, I thought this might be something about the file permissions. So I made 'webapp' the owner of public folder and changed it's mode to 777. The javascripts and css still wont load. No error is shown in the network tab of browser dev tool.
I do I go on and solve this. I am finding it hard to find an approach.
Here's a screenshot of the result of ls -l in the root directory of my app.

Image in storage/app/public returns 404 when trying to get it in index.blade.php

I need an image in my index blade, but I've never done this in Laravel. I keep getting 404's back, when I try.
I've tried altering the route in the asset() function multiple times. Nothing seems to work.
<img src="{{ asset('public/storage/img/wvzonline_logo.png') }}" alt="Het logo van WVZ.Online">
I ran the line down here too.
php artisan storage:link
At the moment I get 404's not found. What I want is the image displayed, of course.
First of all please do check if wvzonline_logo.png exists in the folder public/storage/img.
If yes, give this a try -> {{ asset('/storage/img/wvzonline_logo.png') }}
As the symlink is created successfully you can use the asset() to access the images in the storage folder.
You don't need to prefix public in the asset path, if you do so, you would be accessing http://localhost/public/storage/image.png.
Your http://localhost is already in the public folder scope, so that you only have to access the storage via asset('storage/image.png'). Or you can use Storage::url('image.png') to get the path. However, note that the url is affected by the APP_URL in your .env.
app
config
database
public -|
index.php
storage -|
image.png
storage
app
public
image.png
The config/filesystems.php specified the path of your storage, and the url to access it. Client are unable to view the image unless you put it in the public folder (by php artisan storage:link, which symlinks your storage folder with public/storage).

Laravel 5.6 file not found in storage

My Laravel application is about uploading and showing images. It was working good and all images where shown correctly. Now a few weeks later I tried to test it again and it stopped working. Images are in Laravel storage and path is correct but it says: "Failed to load resource: the server responded with a status of 404 (Not Found)".
Path is: http://localhost:8000/storage/images/8/uploaded/1536930406sunflower.png, and it's correct, image is there.
Weird is that it was working for months and now images cannot be found.
Thanks!
I have found the solution to my question and for those who have the same problem:
Storage link in public folder can be possibly broken.
1)Delete storage link in app/public folder.
2)Run command php artisan storage:link to create storage link again.
Also, you have to be sure about your upload folder name and database image addresses are the same.
Otherwise, it will broke and do not forget that it is case sensitive.Example: If image address is "jpg/photo1.jpg" on DB. Your upload folder in storage folder must be "jpg" not "JPG".

Storing images in storage folder of laravel, and fetching it by a http request is not working

I have deployed laravel application in heroku. I am storing images in storage folder of laravel and trying to fetch it by defining a route. It was working well on local, but when i trying to do the same on server, then its not showing the images. I thought that storage folder is inside the laravel, so it should work fine, but its not working.
Can i store images in public folder by creating a named folder "gallery" inside that and put that gallery folder in gitignore. so will the gitignore folder be vanished when i will push the laravel project again?
Other options are using another file system. Most of them are paid, so i was thinking to save the images in database, is that the good idea or i should move to files only.
First of all, the storage directory should have right permission.
You should create a symbolic link from public/storage to storage/app/public.
Create the symbolic link:
php artisan storage:link
You cannot save images in database, only their names or links etc. I implemented the same thing you did by naming the files to their path and storing that to the database and then using it to fetch the data from folder on sever

(LARAVEL) I am simply trying to link to a photo in my storage/app/images directory

How do I link to my storage directory or should I store these somewhere else. I am creating a basic cms and would like to be able to show photos on each post. right now I am trying
<img src="{{ Storage::get('images/53bb15cceac4f7ecf9fb51cb78296f68.jpeg') }}"
class="img-responsive" alt="Cinque Terre" width="304" height="236">
What happens right now is it returns absolutely nothing.
Images must be stored in a directory that is publicly available to the world. The storage directory should not be publicly available.
You should create a folder somewhere under your /public directory and put your images there (for example /public/images). Then you can link to them like:
<img src="{{ asset('images/53bb15cceac4f7ecf9fb51cb78296f68.jpeg') }}" class="img-responsive" alt="Cinque Terre" width="304" height="236">
Now, you can store images in the storage directory, but you would have to create a Laravel route that has logic to retrieve the data of the image and stream it back as image data, however that would add a lot of complexity just to view an image, and would mean having to load the entire Laravel framework for every image, which is not very efficient.
Edit
Since you're dealing with user uploaded images, you may want to follow some of the advice provided by the Laravel documentation. By default, the store($location) method will place the file in the storage/app/{$location} directory.
However, Laravel comes with a preconfigured public storage filesystem. If you pass in this filesystem as the second parameter, (store($location, 'public')), it will store the file in the storage/app/public/{$location} directory. To make this directory publicly available, you can run the storage:link artisan command, which will create a /public/storage symbolic link to your /storage/app/public directory.
php artisan storage:link
So, if your code is $request()->file('photo')->store('images', 'public'), it will store the photo as /storage/app/public/images/53bb15cceac4f7ecf9fb51cb78296f68.jpeg and you access via {{ asset('storage/images/53bb15cceac4f7ecf9fb51cb78296f68.jpeg') }} (assuming you've run the artisan command to setup the symbolic link).
Documentation on storing uploaded files
Documentation on the public filesystem disk

Resources