Laravel Cannot Access Image Stored in Storage Folder after Uploading in Server
Asked Answered
A

5

15

I am trying to upload and retrieve image in my Laravel project. It worked perfectly in my local server, but after uploading in live server, its not working.

I am using nginx.

I have tried php artisan storage:link but it says

The "public/storage" directory already exists

I am using this line to open the stored image in a new page.

<a href="{{ url('/storage/images/'.$file->file_name) }}" title="">View</a>

If I place mouse on the view button, it shows this link: my_ip/storage/images/image.png which I think is correct.

However, clicking the link redirects to homepage.

Apportionment answered 25/3, 2018 at 7:18 Comment(16)
checked permissions?Remnant
777 for storage folder. do I need to set permission somewhere else?Apportionment
clear cache it may solve your problem command: php artisan clear:cacheMadagascar
is it recursively 777 ?Remnant
@yehia yes, recursively...Apportionment
@Tayyab cache is clearedApportionment
hmm... but I can access in local server. any way to work around? there must be a way to access stored images in storage folder in live server, right?Apportionment
I will try to reproduce your problem. please tell me what OS do you use locally and what on server? I suppose server is ubuntu or some other linux distroNolita
local is windows 10... server is ubuntu 16.04 with nginx and php 7.2Apportionment
Let me know if you need more infor or code I usedApportionment
can you access the contents of public folder with your IP? The URL might be: your_ip/your_project/public/storage/images/image.png check your nginx configuration...Hodges
let me try this...Apportionment
Not working....Apportionment
Did you try: {{ asset('/filepath/from/public' ) }} Use asset() not url()Ductile
Same result. redirected to homepage... :(Apportionment
Thank you so much. I already got the issue. I had to delete and re-link storage folder with public folderApportionment
S
17

The href will be correct, but that's not the issue. The issue is the presence of the file at the location of the link. It's not there, as you're finding.

When you upload, make sure you are not uploading a real directory at public/storage.

On your local server, did you, by mistake, create a real directory at public/storage?

If you did, you need to know that the laravel convention is to store your files in the storage/app/public directory in your app directory. You don't create the folder public/storage yourself. You create a symlink to link it to there instead. That way, stuff you put in storage/app/public, also appears, because of the symlink, at public/storage.

First check your local server follows the laravel convention outlined above (and in the docs), then, after uploading to your server, try the storage:link command again, and, so long as you don't have a real directory at public/storage anymore, but just a link, it should hopefully work.

Note what I've done here is interpret the error message you were getting about that directory already existing.

Also, check this answer if you are using Homestead. Instead of creating a storage link on the host computer, you should ssh into Vagrant and create a storage link here.

Sams answered 25/3, 2018 at 8:58 Comment(6)
I followed the Laravel doc and used php artisan storage:link command to create the symbolic link. and ran the command after uploading the app to server... any suggesion?Apportionment
Try to find out why the public/storage directory already exists.Sams
It was there in my local server. I uploaded it as it was. Should I delete and try the link command again?Apportionment
I deleted the public/storage folder and run php artisan storage:link command again. I don't know why but its working. I guess I need to link public folder with storage each time I upload or install the app in a server.Apportionment
Glad you got it sorted. You could look into creating a composer post-install command if you like, which is a little hook to run after a composer install where you can run short commands like this, so you don't have to run them manually.Sams
I just deleted the public/storage folder which I created mistakenly as you said. After deleting it run the php artisan storage:lin and it's working. Thanks @SamsGrate
A
17

In case you have uploaded public/storage folder to server and getting error: The "public/storage" directory already exists.

just delete it with

rm -R public/storage

and run the command again

php artisan storage:link
Altorelievo answered 7/1, 2020 at 16:35 Comment(0)
S
3

If you are still stuck on this question, this is what worked for me:

In my case I had to change the APP_URL in .env file to the name of my site. It was defaulted to http://localhost. Once I change it everything worked perfectly.

Spae answered 23/4, 2021 at 13:25 Comment(0)
M
1

I had this issue once, its trying to get to my "localhost:/storage/...."

I noticed that it's localhost not localhost:8000, just change your env

from

APP_URL=http://localhost

to

APP_URL=http://localhost:8000
Mendenhall answered 14/9, 2023 at 2:48 Comment(0)
C
0

I ran into the same issue, but had a slightly different resolution than what has already been mentioned here.

In my case, I had run php artisan storage:link from the host computer of my Docker solution. I needed to run it instead from within the context of the Docker container. A sensible solution, but one that's hard to track down, so I figured I'd share.

Cutcliffe answered 18/9, 2023 at 14:54 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.