Using the Storage Facade
Rather than working with raw PHP file functions, Laravel provides the Storage facade — a single, consistent API for reading, writing, and managing files, regardless of whether they live on the local disk or a cloud provider.
Why the Storage Facade Matters
Because the same methods work across every configured disk, switching an application from local storage to a cloud provider like Amazon S3 is mostly a configuration change, not a rewrite of file-handling code throughout the application.
Common Storage Methods
| Method | What It Does |
|---|---|
Storage::put('file.txt', $contents) | Saves content to a file |
Storage::get('file.txt') | Retrieves a file's contents |
Storage::exists('file.txt') | Checks whether a file exists |
Storage::delete('file.txt') | Removes a file |
Storage::disk('s3')->put(...) | Targets a specific storage disk explicitly |
Working with Uploaded Files Specifically
- An uploaded file can be stored directly using its own
store()method - Laravel can automatically generate a unique filename during storage
- Disks are configured centrally in
config/filesystems.php
Storing files is only half the picture — sooner or later, those same files need to be served back to users, which brings us to managing file downloads.
Ready to master Laravel Training Course?
Join Uncodemy's hands-on Laravel program and build real, production-ready applications.
