php-standard-library/filesystem
Type-safe filesystem helpers for PHP with consistent exception handling. Provides safer wrappers for common file and directory operations, aiming for clearer intent and fewer runtime surprises. Part of the PHP Standard Library ecosystem.
Storage facade lacks type safety or granularity (e.g., Directory::ensure() vs. Storage::makeDirectory()). Its event system can integrate with Laravel’s ecosystem but risks redundancy if not scoped (e.g., use for audit logs, not core business events).Storage in HTTP-bound workflows. The PathResolver can standardize path logic across services, reducing base_path()/storage_path() sprawl.Storage (e.g., file_put_contents’s @ operator). However, its lack of cloud storage support limits use in production asset pipelines.FilesystemEvent dispatcher can coexist with Laravel’s events if namespaced (e.g., \App\Events\Filesystem\FileCreated), but requires explicit bridging to avoid duplication.app/Helpers/PathHelper) and manual loops with PathResolver and BatchProcessor.Storage facade usage in HTTP controllers (avoid; use only in CLI/services).File::read() mirror Storage::get(), but the package’s exceptions improve observability. Mitigate by documenting when to use each (e.g., Filesystem for local ops, Storage for cloud).PathResolver can unify path construction across the codebase, reducing DIRECTORY_SEPARATOR hacks.FilesystemEvent could compete with Laravel’s event system (e.g., fileCreated vs. FileUploaded).\\server\share) or long paths (>260 chars) may need additional validation.Directory::ensure() auto-creates directories, which may mask permission issues in shared hosting.app/Helpers/PathHelper)?FilesystemEvent for business logic (high risk) or only infrastructure (e.g., logging)?fileCreated → FileUploaded)?BatchProcessor faster/slower than manual loops for our typical dataset sizes?FileNotFoundException) in CLI tools?File::read())?Filesystem methods (e.g., Directory::ensure()).BatchProcessor for bulk file processing (e.g., image resizing).FilesystemEvent for reactive workflows (e.g., audit logs).Storage/Filesystem calls.$filesystem->on('fileCreated', fn($event) =>
event(new \App\Events\FileCreated($event->getPath()))
);
base_path()/storage_path() with PathResolver in new services.app/Helpers/PathHelper).BatchProcessor.FilesystemEvent for logging/audit use cases.Filesystem and Storage for the same operation (e.g., don’t use both for uploads).Filesystem for local ops, Storage for cloud/cloud-adjacent ops.league/flysystem or symfony/filesystem (disjoint APIs).spatie/laravel-medialibrary (use Filesystem for local assets).symfony/event-dispatcher.composer.json:
{
"require": {
"php-standard-library/filesystem": "^6.2"
},
"extra": {
"aliases": {
"symfony/event-dispatcher": "illuminate/events"
}
}
}
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->bind(\PHPStandardLibrary\Filesystem\Filesystem::class, function () {
$fs = new \PHPStandardLibrary\Filesystem\Filesystem();
if ($this->app->bound('events')) {
$fs->setEventDispatcher($this->app['events']);
}
return $fs;
});
}
// Before
$path = base_path("storage/app/{$id}.txt");
// After
$path = app(\PHPStandardLibrary\Filesystem\PathResolver::class)
->resolveRelativeToProjectRoot("storage/app/{$id}.txt");
// Before
foreach (Storage::allFiles('public/uploads') as $file) {
ProcessFileJob::dispatch($file);
}
// After
$batch = new \PHPStandardLibrary\Filesystem\BatchProcessor();
$batch->process(Storage::allFiles('public/uploads'), 20, fn($file) =>
ProcessFileJob::dispatch($file)
);
How can I help you explore Laravel packages today?