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:
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:
php artisan storage:link
, I manually typed in the symlink('/home/account/laravel-project/storage/app/public','/home/account/api.domain.com/storage')).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>
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.
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:
sudo find /path/to/your/root/directory -type f -exec chmod 644 {} \;
sudo find /path/to/your/root/directory -type d -exec chmod 755 {} \;
sudo chown -R www-data:www-data /path/to/your/root/directory
Make sure after this that you are in group www-data.
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.