Public disk and symbolic links let your Laravel app share files like images or documents with the web browser safely and easily.
Public disk and symbolic links in 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.
php artisan storage:link
<?php use Illuminate\Support\Facades\Storage; // Store a file in the public disk Storage::disk('public')->put('example.txt', 'Hello world!');
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.
<?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.
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.
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.
Practice
public disk in Laravel's filesystem?Solution
Step 1: Understand the role of the public disk
The public disk is designed to hold files that need to be accessed by users through the web, such as images or documents.Step 2: Compare with other storage uses
Other disks like local or private are for internal use, not for public access.Final Answer:
To store files that should be accessible publicly via the web -> Option AQuick Check:
Public disk = public files [OK]
- Confusing public disk with private storage
- Thinking public disk stores config or cache files
- Assuming public disk is for backups
public/storage to storage/app/public?Solution
Step 1: Recall the correct Artisan command
Laravel usesphp artisan storage:linkto create the symbolic link for public storage access.Step 2: Verify other options
Other commands listed are not valid Laravel Artisan commands for this purpose.Final Answer:
php artisan storage:link -> Option DQuick Check:
Storage link command = storage:link [OK]
- Using incorrect Artisan command names
- Confusing 'make' or 'create' with 'storage:link'
- Trying to create symbolic links manually without Artisan
Storage::disk('public')->url('file.txt') return if the symbolic link is correctly created?
use Illuminate\Support\Facades\Storage;
$url = Storage::disk('public')->url('file.txt');
echo $url;Solution
Step 1: Understand Storage::url() behavior on public disk
When the symbolic link is created,Storage::url()returns a URL starting with '/storage/' pointing to the public folder.Step 2: Confirm the full URL format
The method returns a full URL including the app's base URL, so it looks like 'http://your-app-url/storage/file.txt'.Final Answer:
http://your-app-url/storage/file.txt -> Option BQuick Check:
Storage::url() returns public URL [OK]
- Expecting a file system path instead of URL
- Confusing storage path with public URL
- Missing the base URL in the returned string
php artisan storage:link but your files in storage/app/public are not accessible via the browser. What is the most likely cause?Solution
Step 1: Check the symbolic link existence and target
If the symbolic link is missing or points incorrectly, files won't be accessible viapublic/storage.Step 2: Consider other causes but focus on link
While file permissions matter, the most common cause after running the command is a failed or incorrect link creation.Final Answer:
The symbolic link was not created or points to the wrong folder -> Option CQuick Check:
Broken or missing link = no public access [OK]
- Assuming cache clearing fixes symbolic link issues
- Ignoring file permissions as a cause
- Not verifying the link target folder
storage/app/public/images via the web. Which steps correctly set this up in Laravel?Solution
Step 1: Create symbolic link for public storage
Runningphp artisan storage:linkcreates a link frompublic/storagetostorage/app/public, making files accessible.Step 2: Access images via the public URL path
Files insidestorage/app/public/imagesare then accessible at/storage/images/filename.jpg.Final Answer:
Run php artisan storage:link and access images via /storage/images/filename.jpg -> Option AQuick Check:
Symbolic link + /storage/ URL = public access [OK]
- Moving files manually instead of linking
- Not using the symbolic link and expecting direct access
- Overcomplicating with custom routes unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
