Asked 1 month ago by PulsarSurveyor440
How can I make a custom Laravel storage folder for images publicly accessible?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by PulsarSurveyor440
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I've stored my images in the following directory:
storage\app\images\
I need to serve this folder publicly, but I can’t move the images into storage/app/public for reasons beyond this question.
In my controller, I create an "image disk" and store the image as follows:
PHPStorage::makeDirectory('images'); Storage::disk('images')->put($imagename, $image);
Then, in config/filesystems.php, I added another disk:
PHP'images' => [ 'driver' => 'local', 'root' => storage_path('app/images'), 'url' => env('APP_URL') . '/storage', 'visibility' => 'public', ],
There is already a symlink to storage/app/public. How do I make the images directory public?
I tried accessing the image using:
HTML<img src="{{url('storage/app/images/image.jpeg')}}">
This returns an error (image not found).
I also attempted to create a symlink manually:
BASHln -s storage/app/images public/storage/app/images
EDIT:
Notice that I cannot store images in:
storage/app/public
The only available directory is:
storage\app\images\
Additionally, my .env contains:
FILESYSTEM_DRIVER=local
The error I get is:
404 Not Found
https://testSite/storage/app/images/image.jpeg
Comments 0
•Answers 2
•Views 49
Answer by TychoAI Agent • 1 month ago
Below is one approach to achieve public access:
Create a Custom Symlink
Since Laravel’s built-in storage link only covers storage/app/public, you’ll need to create a new symlink for your images folder. For example, from your project root run:
BASHln -s $(pwd)/storage/app/images $(pwd)/public/images
This creates a public directory named “images” that points to storage/app/images.
Update Disk Configuration
Next, update your disk configuration (in config/filesystems.php) so the URL points to the new symlink:
PHP'images' => [ 'driver' => 'local', 'root' => storage_path('app/images'), 'url' => env('APP_URL') . '/images', // Notice the change here from '/storage' 'visibility' => 'public', ],
Accessing the Image in Blade
With the symlink and config in place, you can now reference your images using the correct URL. For example:
HTML<img src="{{ asset('images/image.jpeg') }}">
Alternatively, if you want to generate the URL using the storage disk:
PHP<img src="{{ Storage::disk('images')->url('image.jpeg') }}">
This configuration makes the contents of storage/app/images accessible via your public/images URL without needing to move images into storage/app/public.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
Answer by CelestialScientist918 • 1 month ago
Sorry for the delay in my response. I am using Laravel 11 in a project, but from what I remember and have seen in the documentation, the way to register disks for file uploads in a Laravel project has not changed.
1. Create the Laravel Disk
In the config/filesystems.php
file, create the disk you want:
In this case, I am creating the FAQs disk, but as you requested, it is outside the "storage/app/public" folder. In this case, it will be in "storage/app/faqs".
PHPreturn [ 'disks' => [ 'faqs' => [ 'driver' => 'local', 'root' => storage_path('app/faqs'), 'url' => env('APP_URL') . '/faqs', 'visibility' => 'public', ], ], ];
2. Add the Symbolic Link at the End of the config/filesystems.php
Finally, you need to establish the symbolic link so that Laravel registers it when the command is executed.
PHP'links' => [ public_path('storage') => storage_path('app/public'), public_path('faqs') => storage_path('app/faqs'), // Add yours here, important to establish the same file hierarchy as before ],
3. Execute the Laravel Command
Now, execute the following command:
BASHphp artisan storage:link
This should generate the symbolic link to the new disk created in "storage". In this case, we have created a disk at the path storage/app/faqs
.
No comments yet.
No comments yet.