Installation:
composer require windwalker/filesystem ^4.0
Register the service provider in config/app.php under providers:
Windwalker\Filesystem\FilesystemServiceProvider::class,
First Use Case: Use the filesystem facade to interact with files:
use Windwalker\Filesystem\Facades\Filesystem;
// Create a file
Filesystem::put('path/to/file.txt', 'Hello, Windwalker!');
// Read a file
$content = Filesystem::get('path/to/file.txt');
// Check if a file exists
if (Filesystem::exists('path/to/file.txt')) {
echo "File exists!";
}
Where to Look First:
src/Filesystem.php for core methods.src/FilesystemManager.php for managing multiple filesystem disks.File Operations:
// Write to a file
Filesystem::put('file.txt', 'Content');
// Append to a file
Filesystem::append('file.txt', 'New content');
// Delete a file
Filesystem::delete('file.txt');
// Copy/Move files
Filesystem::copy('source.txt', 'destination.txt');
Filesystem::move('source.txt', 'new_location.txt');
Directory Operations:
// Create a directory
Filesystem::makeDirectory('path/to/dir');
// List directory contents
$files = Filesystem::files('path/to/dir');
// Delete a directory
Filesystem::deleteDirectory('path/to/dir');
Filesystem Disks:
Configure multiple disks (e.g., local, s3) in config/filesystems.php:
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'bucket' => env('AWS_BUCKET'),
],
],
Use them via the manager:
Filesystem::disk('s3')->put('remote-file.txt', 'Remote content');
File Content Manipulation:
// Get file contents as JSON
$json = Filesystem::json('config.json');
// Write JSON to a file
Filesystem::json('config.json', ['key' => 'value']);
Symlinks:
Filesystem::createSymlink('target', 'link-name');
Filesystem::isSymlink('link-name');
Illuminate\Contracts\Filesystem\Filesystem).Windwalker\Filesystem\FilesystemAdapter to create custom storage drivers.filesystem.created, filesystem.deleted) via Laravel’s event system.Path Handling:
storage_path() and public_path() helpers work as expected.$path = storage_path('app/file.txt');
Filesystem::put($path, 'Content');
Permission Issues:
www-data, nginx) has write permissions for directories like storage/ and bootstrap/cache/.Filesystem::chmod() to adjust permissions if needed:
Filesystem::chmod('file.txt', 0644);
Driver-Specific Quirks:
.env. Test connections with:
Filesystem::disk('s3')->url('file.txt'); // Throws exception if misconfigured.
Large File Handling:
Filesystem::stream() to avoid memory issues:
Filesystem::stream('large-file.zip', function ($handle) {
// Process the file stream
});
Enable Logging: Add debug logs for filesystem operations:
Filesystem::debug(true); // Enable debug mode
Check logs in storage/logs/laravel.log.
Check Disk Availability: Verify disks are properly configured:
if (!Filesystem::disk('s3')->exists()) {
throw new \RuntimeException('S3 disk is not configured.');
}
Common Errors:
FileNotFoundException: Double-check paths and disk configurations.PermissionException: Adjust file/directory permissions or use sudo for critical operations.ConnectionException: Validate credentials and network connectivity for remote disks (e.g., S3).Use Collections: Leverage Laravel’s collection methods for file operations:
$files = collect(Filesystem::files('path/to/dir'))
->map(fn ($file) => Filesystem::basename($file))
->toArray();
Custom File Extensions: Extend the package by creating a custom adapter:
namespace App\Filesystem;
use Windwalker\Filesystem\FilesystemAdapter;
class CustomAdapter extends FilesystemAdapter
{
public function customMethod()
{
// Custom logic
}
}
Register it in config/filesystems.php:
'custom' => [
'driver' => 'custom',
'class' => App\Filesystem\CustomAdapter::class,
],
Performance:
Filesystem::delete(['file1.txt', 'file2.txt']);
Filesystem::lastModified() to cache file metadata and avoid repeated checks.Testing:
Storage facade or mock the Filesystem facade in tests:
$this->mock(Filesystem::class)->shouldReceive('put')->once();
How can I help you explore Laravel packages today?