skip to Main Content

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


  1. .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

    Login or Signup to reply.
  2. 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 and Group specify what user/group to run the Apache process. I’ll just refer to this setting as User below.

    Forbidden
    You don’t have permission to access this resource.

    This means the Apache server can not read the index file. There are a number of possibilities, so I’ll run each down:

    • Your User doesn’t have permission to the index.php or index.html in that directory. Change it’s permissions to 644.
    • Your User doesn’t have execute permission to the parent directories. Make sure to grant +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.

    Server unable to read htaccess file, denying access to be safe.

    This means your .htaccess is not readable by your User. It is a good idea to have the .htaccess owned by root and the group set to your Group setting. Then make sure it has 644 permissions.

    Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.

    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 or Group.

    Login or Signup to reply.
  3. The issue you are facing could be due to incorrect file permissions for the storage folder. Here are some steps to fix the problem:

    1. SSH into your cPanel server.

    2. Change the permission of the storage folder to 755:

      chmod -R 755 storage/
      

    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.

    1. 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):

       ls -l
      

    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.

    1. If the ownership is incorrect, you can change it using the following command:

        chown -R www-data:www-data storage/
      

    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.

    1. Finally, make sure that the file upload path is correct. Check that the $model[0] and $data[‘id’] variables have the correct values and that the public directory is at the root of the web server.
      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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search