durimjusaj/gaufrette
Filesystem abstraction layer for PHP via Gaufrette. Develop against a unified API and swap storage backends (local, S3, etc.) without changing application code. Includes maintained adapter metapackages with required dependencies and docs.
Install the Base Package:
composer require gaufrette/gaufrette
Use metapackages for adapters (e.g., gaufrette/aws-s3-adapter) to avoid manual SDK dependencies.
Choose an Adapter:
For Laravel, start with the Local or AwsS3 adapter (most common). Example for S3:
composer require gaufrette/aws-s3-adapter
Basic Setup:
use Gaufrette\Filesystem;
use Gaufrette\Adapter\AwsS3\AwsS3Adapter;
$client = new Aws\S3\S3Client([
'version' => 'latest',
'region' => 'us-east-1',
'credentials' => [
'key' => 'YOUR_KEY',
'secret' => 'YOUR_SECRET',
]
]);
$adapter = new AwsS3Adapter($client, 'your-bucket-name');
$filesystem = new Filesystem($adapter);
First Use Case: Upload a file:
$file = fopen('path/to/local/file.txt', 'r');
$filesystem->write('remote/file.txt', $file);
fclose($file);
Download a file:
$contents = $filesystem->read('remote/file.txt');
Laravel Integration (Optional):
Use KnpGaufretteBundle for Symfony-style config or wrap Gaufrette in a Laravel service provider:
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton('gaufrette', function () {
$adapter = new LocalAdapter('/path/to/storage');
return new Filesystem($adapter);
});
}
KnpGaufretteBundle for Symfony-style configuration examples.LocalAdapter (fast, no dependencies).
$adapter = new LocalAdapter(storage_path('app/gaufrette'));
AwsS3Adapter, AzureBlobStorageAdapter, etc.
$adapter = new AwsS3Adapter($s3Client, 'bucket-name', [
'prefix' => 'uploads/', // Optional: simulate "folders"
]);
// Upload from local file
$filesystem->write('user/123/avatar.jpg', fopen('local.jpg', 'r'));
// Download to local
$contents = $filesystem->read('user/123/avatar.jpg');
file_put_contents('local_copy.jpg', $contents);
$stream = $filesystem->readStream('large-video.mp4');
// Pass to FFmpeg, browser, or queue for processing
$filesystem->mkdir('user/123'); // Creates nested dirs
$filesystem->delete('user/123'); // Deletes directory and contents
foreach ($filesystem->keys() as $key) {
echo $key; // e.g., 'user/123/avatar.jpg'
}
$metadata = $filesystem->getMetadata('user/123/avatar.jpg');
// Returns keys like 'size', 'mime_type', 'last_update'
gaufrette/extras package for ResolvableFilesystem:
$resolvable = new ResolvableFilesystem($filesystem, 'https://example.com/files/');
$url = $resolvable->url('user/123/avatar.jpg');
Service Provider:
public function register()
{
$this->app->bind('gaufrette', function () {
$adapter = config('gaufrette.adapter');
return new Filesystem($adapter);
});
}
Facade:
// app/Facades/Gaufrette.php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class Gaufrette extends Facade { protected static function getFacadeAccessor() { return 'gaufrette'; } }
Usage:
Gaufrette::write('file.txt', 'content');
Filesystem Manager:
Extend Laravel’s FilesystemManager to support Gaufrette:
// config/filesystems.php
'gaufrette' => [
'driver' => 'gaufrette',
'adapter' => function () {
return new LocalAdapter(storage_path('app/gaufrette'));
},
];
Environment-Specific Adapters:
// config/gaufrette.php
'adapters' => [
'local' => LocalAdapter::class . '::create(storage_path("app/gaufrette"))',
's3' => AwsS3Adapter::class . '::create($s3Client, "bucket-name")',
],
'default' => env('APP_ENV') === 'local' ? 'local' : 's3',
StreamWrapper for gaufrette:// URLs:
Enable PHP’s StreamWrapper for seamless file_get_contents('gaufrette://file.txt'):
$filesystem->mount('gaufrette', '/');
StreamContext::registerStreamWrapper('gaufrette', new StreamWrapper($filesystem));
Queue Large File Processing: Use Laravel Queues to process files asynchronously:
$filesystem->write('user/123/video.mp4', $stream);
ProcessVideoJob::dispatch('user/123/video.mp4');
Symfony Bundle (Advanced):
For Symfony-like configuration, use KnpGaufretteBundle and adapt it to Laravel’s service container.
Adapter-Specific Quirks:
AwsS3Adapter::createBucket() if needed.FTP_USEPASSVADDRESS) may fail on older PHP versions (<5.6.18). Use PhpseclibSftp for SFTP.StreamWrapper Security:
gaufrette:// URLs to prevent ../../../etc/passwd attacks.
if (strpos($path, '../') !== false) {
throw new \RuntimeException('Invalid path');
}
gaufrette:// URLs to untrusted users.Metadata Inconsistencies:
Local) may not support all metadata keys (e.g., mime_type). Fall back to mime_content_type() for local files.Deprecated Methods:
AwsS3::getUrl() (deprecated). Use ResolvableFilesystem from gaufrette/extras instead.PHP Version Limits:
Adapter-Specific Errors:
debug: true in the S3 client.ftp_debug mode:
$adapter = new FtpAdapter('ftp.example.com', 'user', 'pass', '/remote/path', [
'debug' => true,
]);
Streaming Issues:
$stream = $filesystem
How can I help you explore Laravel packages today?