Laravel 403 error when displaying images from storage folder

4.2k Views Asked by At

I am unable to access files saved to the storage folder. I'm able to upload, files, save files. If I run for example the size method it gets the file size of the uploaded image but when it comes to displaying the file, I get a 403 error. I used the laravel artisan command to create the symlink, I've tried manually creating the symlink. I've checked to verify that follow symlinks is in my apache config, I can cd into it from shell the permissions are 777 (I had it 755 but in trying to figure it what is wrong I changed it to 777) ownership of the symlink and files inside are all the same user and group as every other file in the public directory.

I'm super tired so maybe I'm just missing something obvious, but I can't for the life of me figure out what is wrong. The file clearly exists, its visibility set to "public". Is there any reason why I'd be able to write the directory but not display images saved there?

Edit 1: web

  • app
  • bootstrap
  • config
  • database
  • error
  • node_modules
  • public
  • resources
  • routes
  • stats
  • storage
  • temp
  • vendor
  • Is the basic structure, with a symlink inside public pointing at storage/app/public

    the filesystems for my storage folder config is:

    'public' => [
                'driver' => 'local',
                'root' => storage_path('app/public'),
                'url' => env('APP_URL').'/storage',
                'visibility' => 'public',
            ],
    

    I haven't really edited anything from the basic laravel install at this point. I did read someone else having a similar problem, said their issue is they weren't allow to access direcotires outside of their document root. So they just had all uploads go to their public folder instead of using the storage folder. I do have my document root set to my public folder. Could that be a problem? (I can edit my apache file if needed)

    3

    There are 3 best solutions below

    3
    On

    Ok - got some sleep and this morning looked over everything and realized that when logged in as the site owner, everything looks fine, however when logged in as root it shows the link as broken. Basically artisan creates an absolute link, which /storage/app/public is fine as the site owner because its a jailkitted account whose "root" directory is the web folder. However it was actually creating a symlink to the system root, of which the normally account doesn't have access to so it was returning a 403

    Basically I just made the as a relative link instead of an absolute one by removing the broken symlink laravel created and while in the public directory entering: ln -s ../storage/app/public storage

    0
    On

    I had the same issue, after a lot of debugging i managed to identify the actual problem that caused 403 error.

    Solution is simple, in

    Config/Filesystem.php where you define your public_path and storage path, you must have same/identical public_path name and storage_path end directory name.

    Example:

    1. Incorrect:
    public_path('brands') => storage_path('storage/app/public/brandimages');
    

    This will generate 403 error, "since public_path('brands')" is not same as "storage_path('../../../brandsimage')".

    1. Correct:
    public_path('brands') => storage_path('storage/app/public/brands');
    

    Now the public_path('brands') and "storage_path('../../../brands')" are same, therefore, correct symlinks will generated,thus solving 403 error.

    Generate symlinks with following artisan command

    php artisan storage:link
    

    if relative links need to be generated, than use following command

    php artisan storage:link --relative
    
    0
    On

    My hosting is a clud server, and my site path is /httpdocs

    The solution worked for me, was:

    from folder /httpdocs/public, execute ln -s ../storage/app/public storage

    then everything works fine.