Laravel 5.6 symlink issues, 404 Not Found, but path actually exists - laravel

I recently deployed my Laravel application to a shared hosting server with ssh access, on my local machine every thing worked perfectly.
but the server is returning the following error:
failed to load resource the server responded with a status of 404 (not
found)
for the images stored in laravel_directory/storage/app/public/
i have successfully created symbolic link to public_html/storage by running
ln -sfv /home/dtruxpgw/repositories/SoukElfalah/storage/app/public/
storage
from the public_html directory
i had also tried both
ln -s /home/dtruxpgw/repositories/SoukElfalah/storage/app/public /home/dtruxpgw/public_html/storage
ln -s /home/dtruxpgw/repositories/SoukElfalah/storage/app/public /home/dtruxpgw/public_html
i have run also
php artisan storage:link
and
chmod 775 -R storage
when i run
ls -la /home/dtruxpgw/public_html/storage
i get
lrwxrwxrwx 1 dtruxpgw dtruxpgw 59 Feb 27 09:04
/home/dtruxpgw/public_html/storage ->
/home/dtruxpgw/repositories/SoukElfalah/storage/app/public/
in the cPanel the storage file exist and linked to laravel_directory/storage/app/public/
here it is the html code
<img class="card-img-top thumbnail" src="{{$img_name?asset('/storage/images/thumbs/'.$img_name) : asset('/storage/images/thumbs/default.png')}}" alt="{{$img_name}}">
i have been stacked here for two days now, any help i would really appreciate it
thank you in advance .

You missed one thing is that you can't access the file system from the storage folder directly as you have written above. If you want to access it publicly upload your documents in public directory.
You can only access files from the storage folder through route only.

Move the thumbs folder with the images into public/img and remove the symlink. It makes no sense to do it like this.. Whatever is to be public should be in the public why should you store images into the storage folder?

Related

Storage symlink doesn't work with Laravel and Vue in Namecheap CPanel (LiteSpeed)

I am trying to access my image files stored inside storage/app/public/subfolder/ using symlink inside /public folder in my Laravel app. Everything works fine in my local setup and also in Jelastic Apache deployment, but it doesn't seem to work in CPanel with LiteSpeed. I am trying to access the files using the link /storage/subfolder/image.png but it is not accessible on CPanel deployment.
I tried creating and deleting storage symlink hundreds of times, but it didn't work. Below are few things I tried:
Deleting and creating symlink again and again using php artisan storage:link command.
Creating symlink using linux command ln -s ../storage/app/public storage inside public folder.
Running Artisan::call('storage:link') using a web route entry.
Redeploying and reconfiguring the app several times and creating storage symlink.
In all of these failed attempts I can verify that storage symlink is created every time inside public folder. I can navigate and view files from public/storage/ folder using CPanel terminal without any issues but can't access them in the deployed app.
I have exact same configuration in Jelastic Apache deployment and in my local deployment and there it is working without any issues.
UPDATE:
Additionally, I tried creating a storage directly manually inside public folder with the same structure and it worked. But symlink doesn't work.
UPDATE 2:
I found below error log while checking error logs from CPanel.
2022-11-06 05:07:07.342556 [ERROR] [1669834] [T0] [HTAccess] Failed to open [/home/user/my-project/public/storage/subfolder/.htaccess]: Permission denied
There is no .htaccess inside subfolder but it is there in my public folder.
What might be the issue?
After making linking you storage to your public path using storage:link you can access your folder :
'Storage_path('subfolder/image.png');'
After a long search, I finally found the cause.
Somehow the storage/app/public directory didn't have sufficient permissions. So, I gave permissions to that public folder recursively using chmod and everything works fine now.
The symlink was working fine but it was not working due to permission issues in the parent folder.
This is a bit weird as I have never manually altered permissions in any Laravel folders in any of my other deployments.

403 error in Laravel storage directory only

I'm currently getting a 403 error on the storage directory.
I have tried the correct file path for both a php artisan link and the regular symlink from command line
The specific path is this one:
http://example.com/storage/app/public/category/zASySdXhEtXBaeWoz4KLJoIVLVxm3W6jRr6gHQhN.jpg
This is linked to the storage directory:
to here: example.com/public/storage/*
I created the symlink like so:
ln -s /var/www/vhosts/example.com/httpdocs/storage /var/www/vhosts/mydomain.com/httpdocs/public
The above directory is for the symlink one but for php artisan link the url is slightly different.
the permissions set for storage before were executed running:
chmod -R 775 storage/
checking permissions I get this:
ls -la ./httpdocs
drwxr--r-- 5 pacer psacln 46 Apr 8 2021 storage
Everything else works except the storage directory. I've seen the other questions regarding 403 and have attempted those fixes but nothing is working. Any idea what I'm doing wrong?
The storage link points directly to the "app/public" folder.
So the link address will look like this :
http://example.com/storage/category/zASySdXhEtXBaeWoz4KLJoIVLVxm3W6jRr6gHQhN.jpg
first : run commint
php artisan storage:link
http://example.com/storage/category/zASySdXhEtXBaeWoz4KLJoIVLVxm3W6jRr6gHQhN.jpg
After a while spent on this the error was caused because I ran the php artisan storage:link and ln -s link with root. Therefore the link was unaccessible by my server for public consumption.
My solution was to switch to user:
su myUser
Then run the php artisan storage:link

laravel symlinks on a window machine

Using a windows server, I have a laravel installation.
I have a storage folder that I have created on 'root/storage'
When I save images from the website, the images are stored in this folder.
I use this to save the files:
Storage::disk('local')->put('course_badges/'.$pre_string . $request->file('badge')->getClientOriginalName(), File::get($request->file('badge')));
When I try to reference the file like so 'https://app.com/storage/file_name.jpg', laravel gives me a 'Sorry, the page you are looking for could not be found.' error.
However is I copy the files to '/public/storage/'
The image is then referenced correctly.
I have run the: php artisan symlink function.
I have tried to run ln -s ../storage/ command in powershell, but windows tells me that ln is not recognized.
What I am trying to achieve is to have my files saved in the root/storage folder (and sub folders)
and to reference them using https:app.nam/storage/file
I had simmilar issue
as u mentioned file is stored in root/storage means problem with symlink between root/storage to public/storage to fix this
remove storage folder from public dir
Note:- keep backup first
then run php artisan storage:link
it should work

Laravel storage symlink on shared hosting

I'm having problem with setting up storage link on shared hosting.
Because of that I can't load any image from storage folder.
My public folder is renamed to web and when I try this:
ln -s storage/app/public/ web/storage
my issue still remains. Also, I have access to the console on the server.
Any help would be appreciated
** Edit -> everything worked fine on localhost (before moving to production)
The problem was with badly linked symlink.
All I had to do was:
ln -sfv ../storage/app/public/ storage from web directory

Laravel Spark is not displaying images that are stored in the /storage/app/public/profiles/ directory

The default for uploaded images appears to be /storage/app/public/profiles/, but the frontend generated URL of /storage/profiles/image isn't being found.
I tried to find any sort of routing for that directory and haven't been able to do so. Any help would be appreciated.
You must link to the storage directory. From the documentation:
Once Spark is installed, you should link the public/storage directory to your storage/app/public directory. Otherwise, user profile photos stored on the local disk will not be available
And the relevant code:
ln -s /path/to/storage/app/public /path/to/public/storage
Just delete the storage folder inside public folder and run the command using absolute paths(from root folder).

Resources