How to Use Public Disk in Laravel: Simple Guide
In Laravel, use the
public disk to store files accessible via the web by configuring it in config/filesystems.php. Use Storage::disk('public') methods to save, retrieve, or delete files in the storage/app/public folder, which is linked to public/storage for public access.Syntax
The public disk is defined in config/filesystems.php and points to storage/app/public. You use it with Laravel's Storage facade like this:
Storage::disk('public')->put('filename.txt', 'contents');- saves a fileStorage::disk('public')->get('filename.txt');- reads a fileStorage::disk('public')->url('filename.txt');- gets the public URL
Make sure to run php artisan storage:link once to create a symbolic link from public/storage to storage/app/public.
php
use Illuminate\Support\Facades\Storage; // Save a file to the public disk Storage::disk('public')->put('example.txt', 'Hello Laravel!'); // Read the file contents $content = Storage::disk('public')->get('example.txt'); // Get the public URL $url = Storage::disk('public')->url('example.txt');
Example
This example shows how to save a text file to the public disk, read its contents, and get its public URL for web access.
php
<?php use Illuminate\Support\Facades\Storage; Route::get('/test-public-disk', function () { // Save a file Storage::disk('public')->put('greeting.txt', 'Hello from Laravel public disk!'); // Read the file $content = Storage::disk('public')->get('greeting.txt'); // Get public URL $url = Storage::disk('public')->url('greeting.txt'); return [ 'file_content' => $content, 'public_url' => $url ]; });
Output
{"file_content":"Hello from Laravel public disk!","public_url":"http://your-app.test/storage/greeting.txt"}
Common Pitfalls
Common mistakes when using the public disk include:
- Not running
php artisan storage:linkto create the symbolic link, so files are not accessible via the web. - Trying to access files directly in
storage/app/publicwithout the link. - Using
Storage::put()without specifying thepublicdisk, which saves files in the default disk (usually local) and not publicly accessible.
php
<?php // Wrong: saves file in default disk, not public Storage::put('file.txt', 'data'); // Right: saves file in public disk Storage::disk('public')->put('file.txt', 'data');
Quick Reference
| Action | Code Example | Description |
|---|---|---|
| Save file | Storage::disk('public')->put('file.txt', 'contents'); | Save a file to public disk |
| Read file | Storage::disk('public')->get('file.txt'); | Get file contents |
| Get URL | Storage::disk('public')->url('file.txt'); | Get public URL for file |
| Delete file | Storage::disk('public')->delete('file.txt'); | Remove file from public disk |
| Create link | php artisan storage:link | Create symbolic link for public access |
Key Takeaways
Use Storage::disk('public') to work with files in the public disk.
Run php artisan storage:link once to make files publicly accessible.
Files saved on the public disk are stored in storage/app/public.
Access files via URLs under public/storage after linking.
Avoid saving files without specifying the public disk if you want web access.