CookieMonster's avatar

is it okay to save uploaded image in public folder?

For example, as an user, I can upload my avatar to my profile and it will saved in public/images/avatar/

Is it better to place it in storage/public/avatar?

If I were to host it in a production server, will it have issues?

0 likes
13 replies
Wakanda's avatar

@nickywan123 you won't have any problems all you need to do is run

php artisan storage:link

and you are good to go

Wakanda's avatar

@nickywan123 if the images are supposed to be accessed by everyone that won't be an issue unless if the images will be private

sr57's avatar

why do I need to run php artisan storage:link if it's saved in public?

You don't need to create the link in this case.

are there any security issues?

No, till you don't change the rights of project directories.

That said, Laravel suggests you to put :

  • data (upload files, ...) in th storage directory

  • public data (file that can be accessed by an url) in public directory

So the link, that Laravel suggests to create, is a way to follow these 2 good practices.

Laravel is an open framework, you can do what you want, but following it's recomandations is a good way to create good project.

Cruorzy's avatar

Directly i cannot think of any issues. But git is setup to ignore the right files so thats a plus.

Whats the reason you want to store it directly in public instead of storage?

CookieMonster's avatar

I usually store files in public. Is there any difference between storing in public vs storage?

And what do people mean if you store in public directory that can be accessed by an url?

sr57's avatar

Hi @nickywan123

See my previous answer, it's only Laravel conventions / good practices.

CookieMonster's avatar

Okay, I just wanted to be sure if it's okay without any issue doing so.

CookieMonster's avatar

Since I saved them in public folder, I tried accessing one of the images like below:

http://forum.test/public/images/uploads/avatars/default.jpeg

and this image exists in the public folder but it returns 404. Am I supposed to be allowed to access this right?

CookieMonster's avatar

I am just confused earlier with what this means:

public data (file that can be accessed by an url) in public directory

I might have misunderstood it as visiting to www.my-domain.com/public/images/xxx should be able for me to view the image(on the browser itself). That's what i understood when you said can be accessed.

sr57's avatar

Ok, sorry, my sentence was not enough clear, you cannot access file directly by an simple url without definig a route (Laravel security); Url works when route (security) is defined and the place (directory) where you put your data is just a convention.

CookieMonster's avatar

Oh okay I think I understand now. So if i defined a route for the image then it shall be viewable right?

Now if that image is saved in storage/images/ then defining the route would not have mattered and we can't see the image when we visit the url (unless we use symbol link). Is this correct?

Please or to participate in this conversation.