Trouble to display images in a laravel project on shared hosting.

Published 1 month ago by se2B

Hi everyone,

I'm stuck on a trouble to access images stored on my personnal server www.sebastienbourda.fr.

Server Config tree:

/
    pepiniere
        app
        ...
        storage
        ...
    www
        ....
        

I tried to create symlink in www to point in storage/ folder but it don''t work.

When i tried to display the page

http://sebastienbourda.fr/storage/images/palmier-pepiniere-des-sables-agen-arbuste.jpg

i've a permission error: Forbidden You don't have permission to access /storage/images/palmier-pepiniere-des-sables-agen-arbuste.jpg on this server.

Best regards guys.

rsvb
rsvb
1 month ago (5,760 XP)

I would not use the storage folder.

se2B

Thanks rsvb. Can you detail steps to change the storage folde? I'm using a storeas method to store picture in storage folder but i don't know how to change for the www/images folder

Thank you.

jlrdw
jlrdw
1 month ago (223,330 XP)

I use shared host with ver 5.5 no problems, see this guide: http://novate.co.uk/deploy-laravel-5-on-shared-hosting-from-heart-internet/

And structure like https://imgur.com/Oo6k4Fp

rsvb
rsvb
1 month ago (5,760 XP)

The image is stored by Laravel?? Then it should work.

se2B

@jlrdw thanks for links. I follow this guide but it seems we need to create symlink to use storage folder and in my case it didn’T work apparently.

m-rk
m-rk
1 month ago (37,160 XP)

As suggested earlier, do not use your storage directory for this. Because as soon as this is going to work I am able to view all of the content of your storage directory if I know the path and because you use Laravel it is not that hard to guess a few path's. That will give me for example the opportunity to read your application logs in /storage/logs/laravel.log which could contain private/sensitive data.

It would be better if you could use your public directory for this. Or if this is somehow not possible at least do not open your full storage directory but only one specific directory.

se2B

@m-rk

If i understand, i need to use storage/app/public (with a symlink).

My question is: "how to access the picture i stock in storage/app/public/images/... since the view?"

It works on the local project but not on the hosted project.

If i need to use a folder in my /www folder, how can i store images in /www/images folder. When i try, i've an error of path.

sorry but it's my first deployment project.

Thanks a lot.

Cronix
Cronix
1 month ago (334,240 XP)

Using php artisan storage:link, it creates a symlink between /storage/app/public and /public/storage. So when you upload files, they get physically stored in /storage/app/public, but accessed by yoursite.com/storage/filename. This is secure and won't allow someone to read anything else from storage that isn't in the /storage/app/public dir, so no one would be able to read log files or anything else. This is actually the way laravel sets up some of their other projects, like Laravel Spark. It's secure.

https://laravel.com/docs/5.6/filesystem#the-public-disk

rsvb
rsvb
1 month ago (5,760 XP)

Remember, if you are using the local driver, all files that should be publicly accessible should be placed in the storage/app/public directory. Furthermore, you should create a symbolic link at public/storage which points to the storage/app/public directory.

Why you use this, I do not see something usefull in this method.

Helmchen

you usually create a symlink from /storage/app/public/[anyfolder] to /public/[anyfolder]

in my case i have an folder storage/app/public/pictures which i link to public/pictures

as public (or www in your case) already is the webserver root - you can access the file with /pictures/[your-file]

se2B

Thanks all guys

@Helmchen i've create the symlink so i've www/images symlink which point to storage/app/public/images but i've an error when i trie to access to www.mydomain.fr/images

error 403 forbidden

jlrdw
jlrdw
1 month ago (223,330 XP)

I never have problems just doing something like:

<td><img src="<?php echo asset('assets/upload/imgdogs') . '/' . $row->dogpic; ?>" alt=""></td>

To note on shared you may have to put under assets. Just resolve the path correctly.

Helmchen

also check permissions on the folder - 0755 seems a bit high, but should work, especially on shared hosting

without a index document (index.html, index.php) you may not have access to view the folder contents - so make sure you are accessing an existing file during your tests

se2B

Ok. I tried something different. I want to store file directly in a folder www/images but when i sepcify in my controller $path = $request->file('picture1')->storeAs('../www/images' . $id_post, "picture1.jpeg"); i've an error "Path is outside of the defined root, path: [../www/images/picture1.jpeg]".

Can you tell me if it's possible and how to do that?

For the "storage folder solution", i don't understand the problem.I've all the permission on folder and create symlink but it doesn't work.

jlrdw
jlrdw
1 month ago (223,330 XP)

To store you use a path, to view you use url. That's just the way it works.

Please sign in or create an account to participate in this conversation.