Public disk and symbolic links let your Laravel app share files like images or documents with the web browser safely and easily.
0
0
Public disk and symbolic links in Laravel
Introduction
You want users to see uploaded images on your website.
You need to serve files stored outside the public folder.
You want to keep files organized but still accessible from the web.
You want to avoid copying files manually to the public folder.
You want to manage file storage with Laravel's built-in tools.
Syntax
Laravel
php artisan storage:link
This command creates a symbolic link from public/storage to storage/app/public.
It allows files in the storage/app/public folder to be accessible via the web.
Examples
Creates the symbolic link so public files are accessible.
Laravel
php artisan storage:link
This saves a file to the public disk, which is linked to the web.
Laravel
<?php use Illuminate\Support\Facades\Storage; // Store a file in the public disk Storage::disk('public')->put('example.txt', 'Hello world!');
Sample Program
This controller lets you upload a photo to the public disk and get a URL to access it via the web.
Remember to run php artisan storage:link once to make files accessible.
Laravel
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; use App\Http\Controllers\Controller; class FileController extends Controller { public function upload(Request $request) { $path = $request->file('photo')->store('photos', 'public'); return "File stored at: " . $path; } public function show($filename) { $url = Storage::disk('public')->url('photos/' . $filename); return "Access your file here: " . $url; } } // Run 'php artisan storage:link' once to create the symbolic link.
OutputSuccess
Important Notes
Always run php artisan storage:link once after setting up your project.
Symbolic links work like shortcuts, so the web server can find your files without moving them.
Make sure your web server has permission to read the storage folder.
Summary
Public disk stores files you want to share on the web.
Symbolic links connect storage files to the public folder safely.
Use php artisan storage:link to create this connection easily.