File, Disks and Storage
-
Difference between
Filesystem
andStorage
- Quick Reference Table
- Storing Files
- Getting Files
- Deleting Files
- Downloading Files
Filesystem
and Storage
Difference between In Laravel, both Filesystem
and Storage
are used to interact with files, but they serve
slightly different purposes and offer different levels of abstraction.
The Filesystem
class provides a set of static methods for interacting with the file system directly.
It includes methods for reading and writing files, creating and deleting directories, and other
low-level operations. It's a more "raw" way of dealing with files and directories, and it doesn't
involve any configuration or abstraction.
The Storage
facade, on the other hand, provides a more abstract way of dealing with file
storage. It allows you to define "disks" in your configuration, which can be local directories,
Amazon S3 buckets, or any other type of storage supported by the Laravel community. The Storage
facade provides a simple, unified API for interacting with these disks, regardless of their
underlying implementation.
In general, you would use the Storage
facade when you want to take advantage of Laravel's
filesystem configuration and abstraction, and the Filesystem
class when you need to perform
low-level file operations.
In short;
-
Filesystem
class is used to perform low-level file operations. -
Storage
facade is used to interact with any of your configured disks.
Quick Reference Table
Action | Storage Facade |
Helper |
---|---|---|
Store a file | Storage::disk('public')->put('path/to/file', $contents); |
$file->store('path/to/file', 'public'); |
Check if a file exists | $exists = Storage::disk('public')->exists('path/to/file'); |
$exists = $file->isValid(); |
Delete a file | Storage::disk('public')->delete('path/to/file'); |
Not available |
Get a file's size | $size = Storage::disk('public')->size('path/to/file'); |
$size = $file->getSize(); |
Get a file's last modification time | $time = Storage::disk('public')->lastModified('path/to/file'); |
$time = $file->getATime(); |
Storing Files
Store vs Save
The storeAs
and store
methods are used to store uploaded files in Laravel. However, they work
slightly differently:
-
storeAs($path, $name, $disk)
: This method stores the file in a directory with a name that you provide. The$path
parameter is the directory where the file will be stored,$name
is the name that the file will be given, and$disk
is the storage disk that the file will be stored on. -
store($path, $disk)
: This method also stores the file, but will automatically generate a unique ID as the filename. The$path
parameter is the directory where the file will be stored, and$disk
is the storage disk that the file will be stored on.
use Illuminate\Support\Facades\Storage;
$request->validate([
'image' => 'image|max:2048', // max size 2MB
]);
$file = $request->hasFile('image'); // Retrieve the uploaded file from the request
$filename = $file->getClientOriginalName(); // Retrieve the original filename
Storage::disk('public')->put('path/to/file', $contents);
$file->store('path/to/file', 'public');
Naming Files
$originalName = $file->getClientOriginalName(); // Retrieve the original filename
$timestampName = time() . '.' . $image->getClientOriginalExtension();
Getting Files
Storage::disk('public')->get('path/to/file');
Deleting Files
use Illuminate\Support\Facades\Storage;
Storage::disk('public')->delete('path/to/file');
Storage::delete(['path/to/file1', 'path/to/file2']);
Downloading Files
return Storage::disk('your_disk_name')->download('path/to/file');
return Storage::download('path/to/file');