I was having trouble with the folder created through a symlink. It was created manually by creating a PHP file and adding the symlink() action because it's deployed on a CPanel host. The public folder is in a different directory than the main Laravel project. Now, I'm trying to access my files through the URL "domain.com/storage/uploads/img/filename.png." The problem is, that it's returning a 404 error.
The structure of the directory is like this:
- The public folder that is shipped with Laravel is in the api.domain.com (We added a subdomain in a different directory) making the directory: /home/account/api.domain.com/
- The Laravel project is in a different folder: /home/account/laravel-project/
To confirm, the file uploaded exists on the folder I am referencing and where the symlink is connected. And I'm accessing the correct URL (hopefully). I also tried opening the symlink folder itself in the File Manager if it's really connected to the folder, and it is!
Here's how I'm saving the file in the controller:
$myFile->storeAs('public/uploads/img', $myFile->getClientOriginalName());
And here's how the path is saved:
$filePath = asset('storage/uploads/img/' . $myFile->getClientOriginalName());
Whenever I try to embed the image, it's broken. And whenever I try accessing the file in my browser, it's returning a 404 error.
Notes:
- The FollowSymLink is enabled on WHM
- I tried recreating the symlink by deleting the old folder and relinking them (I didn't type in the
php artisan storage:link
, I manually typed in the symlink('/home/account/laravel-project/storage/app/public','/home/account/api.domain.com/storage')) - Here's my
.htaccess
:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
Options FollowSymLinks
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (. )/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php [L]
</IfModule>
- This post's problem is similar to mine, but the solution didn't work.
I tried different things by searching other search queries but couldn't get it to work. I'm not sure if I'm missing something or do I need to install a package.
CodePudding user response:
I had similar issue few days ago, even when file was there and symlink was existing and good. What helper me was reassuring all the permissions of laravel files and folders are right. I did next thing:
- I made sure that all files are 644 and folders 755 with chmod (i guess u can change that from cPanel but it would be easier if u have SSH access):
sudo find /path/to/your/root/directory -type f -exec chmod 644 {} \;
sudo find /path/to/your/root/directory -type d -exec chmod 755 {} \;
- Then I made www-data (default web user on ubuntu - on most web providers this is case) default owner of laravel project directory with:
sudo chown -R www-data:www-data /path/to/your/root/directory
Make sure after this that you are in group www-data.
- Then i deleted my bootstrap/cache folder to clear cache. After it was recreated i did:
sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug rwx storage bootstrap/cache
What i guess that have happend is that your permissions are wrong on webserver, that is what was my case. Be sure to run
php artisan optimize:clear
Also, make sure that your domain is set up properly in .env file.
After doing everything so that u can clear all the cache once again. Not sure this will help you without more info about your permissions setup and groups, but surely it's a thing to try.