skip to Main Content

I am trying to access images for my project.

I have run php artisan storage link

Laravel create public/storage

when I upload image to storage/app, I see the image in storage/app but not in public/storage.

When I try to view image on browser, I get error 404

controller.php

Property::create([
'SittingRoom'=> request()->file('sittingroom')->store('apartments')
]);

bladeview.php

<img src="{{asset('storage/'.$duplex->SittingRoom)}}">

browswer link

http://127.0.0.1:8000/storage/apartments/HNLDysVUdRG7ZTBkF2qFGk2B2tAKYzYcVImNEWLx.jpg

after running artisan storage link

C:xampphtdocsOPAAM> php artisan storage:link

INFO The [C:xampphtdocsOPAAMpublicstorage] link has been connected to [C:xampphtdocsOPAAMstorageapp/public].

2

Answers


  1. Chosen as BEST ANSWER

    The problem was that in

    config/filesystems.php

    'default' => env('FILESYSTEM_DISK', 'public'),

    but in

    .env file

    FILESYSTEM_DISK=local

    This made images to be save in

    Storage/app instead of storage/app/public.

    Images saved in storage/app cannot be accessed publicly.

    so i have to change .env file


  2. Why not found public/storage?

    Need to check

    1. IMPORTANT: The symbolic link is not created. This needs to be checked in public folder! It is possible that the command failed to create the symbolic link because a directory/file with the same name already exists in the public directory! Please delete it if there is any such directory/file and run the command again.
    2. IMPORTANT: In .env need FILESYSTEM_DISK=public (as default setting)
    3. IMPORTANT: There may be an .htaccess file that blocks access to any file. You can check for this file in both the public and storage/app/public directories.
    4. It is calling an incorrect path.
    5. The file really does not exist, has the wrong file extension, etc.

    By the way, the asset() helper function is being used correctly and the received link also looks valid.

    I don’t know what the significance of the code snippet displayed in "controller.php" is in displaying an existing file. As I assume you have verified that the file exists.

    How to can check hidden files (as symbolic link)?

    Get a list of hidden files/folders.

    Linux:

    ls -la "<path of project>/public"
    

    Windows:

    dir -h "<path of project>/public"
    


    More information

    About the public/storage directory

    The public/storage directory does not physically exist in the Laravel. All files to be stored should be placed in the storage/app/public directory. This will be the physical directory that you can make available using a symbolic link.

    Symbolic link

    A virtual reference that associates a non-existent path with an existing one. In our example, we can associate the non-existent path public/storage with the physically existing storage/app/public directory.
    How? We have two options for this. Either use the php artisan storage:link command in Laravel specifically designed for this purpose, or manually set it up in the operating system.
    Obviously, the former is the more obvious solution.

    Why is it set up this way?

    In the storage directory, we can store other sensitive files, such as log files, cache files, etc. In addition, we can also store public files that can be made public using the symbolic link if desired.
    The public directory is the directory in the Laravel framework that the user actually accesses. It cannot access any other files except for those with the appropriate documentation configuration. This way, you can control what you want to share.

    Summary

    If you create the symbolic link, it will work as expected. For example, you can access the storage/app/public/images/test.png file through a virtual path: public/storage/images/test.png.

    Documentation: https://laravel.com/docs/10.x/filesystem#the-public-disk

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