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
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
Why not found
public/storage
?Need to check
public
directory! Please delete it if there is any such directory/file and run the command again..env
needFILESYSTEM_DISK=public
(as default setting).htaccess
file that blocks access to any file. You can check for this file in both thepublic
andstorage/app/public
directories.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:
Windows:
More information
About the
public/storage
directoryThe
public/storage
directory does not physically exist in the Laravel. All files to be stored should be placed in thestorage/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 existingstorage/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