I’m using Laravel and uploading files to a folder using this code:
$request->file('file')[$key]->storeAs('public/' . $model[0] . '/' . $data['id'], $name);
I have already run this command:
php artisan storage:link
On localhost everything is working, but when I upload it to cPanel I get this error:
Forbidden
You don't have permission to access this resource.
Server unable to read htaccess file, denying access to be safe
Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.
I checked the uploaded file had permission 700
, and not 755
like the folder $model[0]
.
How can I fix this problem?
My .htaccess
file:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# 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
RewriteRule ^ index.php [L]
</IfModule>
3
Answers
.htacces File: Chmod (to 755) is one option.
Another is to correct the owner or group with Chown to that of the Webserver.
in standard setups mostly called "www-data"
Shell: chown www-data .htaccess (Owner)
or Shell: chown root.www-data .htaccess (Group)
or both Shell: chown www-data.www-data .htaccess (Owner&Group)
But your Error may be related to File permissions of the uploaded file
Maybe you have to define and apply the visibility to the uplaoded files too.
Hace a look into:
https://laravel.com/docs/10.x/filesystem#file-visibility
It should be pointed out, the reported errors are actually multiple separate issues. I’ll address them in order.
First, in your Apache configuration file, the
User
andGroup
specify what user/group to run the Apache process. I’ll just refer to this setting as User below.This means the Apache server can not read the index file. There are a number of possibilities, so I’ll run each down:
index.php
orindex.html
in that directory. Change it’s permissions to 644.+x
. Numerically, that means all the directories back to/
must end in 1, 3, 5, or 7 in the whole path to your Apache<Directory /path/to/directory>
setting.This means your
.htaccess
is not readable by your User. It is a good idea to have the.htaccess
owned byroot
and the group set to your Group setting. Then make sure it has 644 permissions.This means the Apache User doesn’t have permission to read the file or URL pointed to in your
ErrorDocument
setting.Additionaly, you should also make sure your storage directory from Laravel is writable by the Apache
User
orGroup
.The issue you are facing could be due to incorrect file permissions for the storage folder. Here are some steps to fix the problem:
SSH into your cPanel server.
Change the permission of the storage folder to 755:
This command will change the permission of the storage folder and all its contents to 755, which should give the web server permission to read and write to the folder.
Check the ownership of the storage folder. Ensure that the owner of the folder is the same as the user running the web server (typically www-data on Apache):
This command will list the files and folders in the current directory along with their ownership and permission details. Make sure that the owner of the storage folder is the same as the user running the web server.
If the ownership is incorrect, you can change it using the following command:
This command will change the ownership of the storage folder and all its contents to the www-data user and group, which is typically used by Apache.
After making these changes, try uploading a file again and see if the issue is resolved.
After making these changes, try uploading a file again and see if the issue is resolved.