league/flysystem-gridfs
MongoDB GridFS adapter for Flysystem. Install via composer (league/flysystem-gridfs) to use GridFS as a filesystem backend for reading and writing files. This is a split package; issues and PRs are handled in the main Flysystem repo.
Start by installing the package and ensuring required extensions:
composer require league/flysystem-gridfs
Verify mongodb extension (v1.3+ or v2+) is enabled via php -m | grep mongodb. Then instantiate the adapter and Flysystem client:
use League\Flysystem\Filesystem;
use League\Flysystem\GridFS\GridFsAdapter;
$client = new MongoDB\Client('mongodb://localhost:27017');
$adapter = new GridFsAdapter($client, 'myapp', 'fs'); // DB name + collection prefix
$filesystem = new Filesystem($adapter);
The first use case is typically replacing Storage::put()-style operations with GridFS-backed storage—ideal for large or frequently duplicated assets in clustered environments.
FilesystemManager::extend() in Laravel to register GridFS as a disk ('gridfs'), enabling seamless swapping between disks via Storage::disk('gridfs').writeStream()/readStream() for files >16MB to avoid memory exhaustion:
$stream = fopen($request->file('upload')->getRealPath(), 'r');
$filesystem->writeStream('uploads/' . $uuid, $stream);
$filesystem->write($path, $contents, ['metadata' => ['version' => 3]]). GridFS stores this in fs.files, queryable via standard MongoDB tools.LocalAdapter (e.g., serve small files directly, offload large ones to GridFS) using a dispatcher or conditional logic based on file size.files_id, n) for chunk lookups. Run db.fs.chunks.createIndex({ files_id: 1, n: 1 }) manually—missing indexes cause catastrophic slowdowns with large datasets.prefix (e.g., fs) prepended to files/chunks collections. Avoid reuse across apps/databases unless intentional (e.g., prod_fs, staging_fs).mongodb/mongodb (v1.x), which aligns with ext-mongodb v2.x. Avoid legacy mongo (pecl-mongo) apps—migration requires full overhaul of MongoDB connections.chunkCount() ≈ ceil(fileSize / chunkSize)) and alert on discrepancies.Filesystem instances across requests—MongoDB\Client isn’t serializable. Re-instantiate per request or use Laravel’s service container with lazy resolution.How can I help you explore Laravel packages today?