Local disk storage lets your Laravel app save files directly on your server's hard drive. This is useful for storing images, documents, or backups safely and quickly.
Local disk storage in Laravel
use Illuminate\Support\Facades\Storage; // Save a file Storage::disk('local')->put('filename.txt', 'File contents'); // Get a file $content = Storage::disk('local')->get('filename.txt'); // Delete a file Storage::disk('local')->delete('filename.txt');
The 'local' disk stores files in the storage/app folder by default.
Use the Storage facade to interact with files easily.
notes.txt with the text 'Hello Laravel!' inside the local storage folder.Storage::disk('local')->put('notes.txt', 'Hello Laravel!');
notes.txt from local storage and prints it.$content = Storage::disk('local')->get('notes.txt'); echo $content;
notes.txt file from local storage.Storage::disk('local')->delete('notes.txt');
This Laravel controller has three methods: one to save a file named example.txt with some text, one to read and return its content, and one to delete the file. It uses the local disk storage.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; class FileController extends Controller { public function saveFile() { Storage::disk('local')->put('example.txt', 'This is a test file.'); return 'File saved successfully!'; } public function readFile() { if (Storage::disk('local')->exists('example.txt')) { $content = Storage::disk('local')->get('example.txt'); return $content; } return 'File not found.'; } public function deleteFile() { Storage::disk('local')->delete('example.txt'); return 'File deleted successfully!'; } }
Files saved on the local disk are stored in storage/app. You can change this path in config/filesystems.php.
Remember to run php artisan storage:link if you want to make files publicly accessible via public/storage.
Local disk storage is fast but only works on the server where your app runs.
Local disk storage saves files on your server's hard drive inside storage/app.
Use the Storage facade with disk('local') to put, get, and delete files.
This method is great for quick, private file storage during development or on your own server.
Practice
local disk in Laravel's storage system refer to?Solution
Step 1: Understand Laravel's local disk meaning
Thelocaldisk stores files on the server's hard drive, specifically inside thestorage/appdirectory.Step 2: Compare with other storage options
Other options like cloud or FTP are not calledlocal. The local disk is always the server's own storage.Final Answer:
The server's own hard drive insidestorage/app-> Option DQuick Check:
Local disk = server hard drive storage [OK]
- Confusing local disk with cloud storage
- Thinking local disk is temporary memory
- Assuming local disk is remote storage
example.txt with content 'Hello' on the local disk in Laravel?Solution
Step 1: Identify correct method chaining
Laravel usesStorage::disk('local')->put(filename, content)to save files on the local disk.Step 2: Check method and parameter order
Storage::disk('local')->put('example.txt', 'Hello'); correctly callsdisk('local')thenput('example.txt', 'Hello'). Others mix parameters or methods incorrectly.Final Answer:
Storage::disk('local')->put('example.txt', 'Hello'); -> Option BQuick Check:
Use disk('local')->put(filename, content) [OK]
- Swapping parameters in put method
- Calling disk() with filename instead of 'local'
- Using Storage::local() which does not exist
Storage::disk('local')->put('note.txt', 'Hi there');
$content = Storage::disk('local')->get('note.txt');
echo $content;Solution
Step 1: Save content to local disk
Theputmethod writes 'Hi there' into 'note.txt' on the local disk.Step 2: Retrieve and echo the file content
Thegetmethod reads the content of 'note.txt', which is 'Hi there', then echoes it.Final Answer:
Hi there -> Option AQuick Check:
Put then get returns saved content [OK]
- Expecting filename instead of content output
- Assuming get returns boolean
- Forgetting to use disk('local')
Storage::disk('local')->delete('file.txt');Assuming
file.txt does not exist, what happens?Solution
Step 1: Understand delete behavior on missing files
Laravel'sdeletemethod returns false if the file does not exist but does not throw an error.Step 2: Confirm no side effects
No file is created or deleted besides the target. Execution continues normally.Final Answer:
Returns false but no exception is thrown -> Option AQuick Check:
Delete missing file returns false, no error [OK]
- Expecting an exception on missing file
- Thinking delete creates files
- Assuming delete removes all files
Solution
Step 1: Understand putFile and putFileAs methods
putFilestores the file with a unique generated name automatically.putFileAsrequires you to specify the filename.Step 2: Check options for unique naming
Storage::disk('local')->putFile('', $imageFile); usesputFilewhich auto-generates a unique name, preventing overwrites. The distractors use fixed filenames such as 'image.jpg', which risk overwriting existing files.Final Answer:
Storage::disk('local')->putFile('', $imageFile); -> Option CQuick Check:
putFile auto-names files uniquely [OK]
- Using put which overwrites files
- Manually generating names incorrectly
- Using putFileAs without unique names
Ready to prove it?
Pass the test and this pattern moves unknown
