How to Link Storage Folder to Public in Laravel 9?

How to Link Storage Folder to Public in Laravel 9?

In Laravel 9, you can link the storage/app/public folder to the public/storage directory using the following command:

Guide to Link Storage Folder to Public

Step 1: Ensure You Have the Public Disk Configured

By default, Laravel’s filesystem configuration is located in config/filesystems.php. Make sure the 'disks' array contains the following public disk configuration:

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

Step 2: Run the Artisan Command

Execute the following command to create a symbolic link from storage/app/public to public/storage:

php artisan storage:link

Step 3: Verify the Storage Link

Once the command runs successfully, check if the storage folder is now accessible inside the public directory:

ls -l public/

You should see a symbolic link named storage pointing to storage/app/public.

Step 4: Store and Access Files in Public Storage

Now, you can store files in the storage/app/public directory and access them via the URL /storage/{filename}.

Example of storing a file in the public disk:

use Illuminate\Support\Facades\Storage; $file = $request->file('image'); $path = $file->store('uploads', 'public'); echo Storage::url($path); // Outputs: /storage/uploads/filename.jpg

Step 5: Ensure the Correct Permissions (If Needed)

If the storage link is not working, set the correct permissions:

chmod -R 775 storage bootstrap/cache chown -R www-data:www-data storage bootstrap/cache

Step 6: Clear Cache (If Still Not Working)

If the storage link is still not accessible, clear Laravel's cache:

php artisan config:clear php artisan cache:clear php artisan view:clear

Conclusion

Now, your storage folder is successfully linked to the public directory, allowing you to access stored files via the public/storage URL.  Let me know if you need any further assistance! 

Souy Soeng

Souy Soeng

Hi there 👋, I’m Soeng Souy (StarCode Kh)
-------------------------------------------
🌱 I’m currently creating a sample Laravel and React Vue Livewire
👯 I’m looking to collaborate on open-source PHP & JavaScript projects
💬 Ask me about Laravel, MySQL, or Flutter
⚡ Fun fact: I love turning ☕️ into code!

2 Comments

CAN FEEDBACK
  1. s
    s
    (k)
  2. s
    s
    :-b
close