Flysystem
by @frankdejonge
Flysystem is a filesystem abstraction which allows you to easiliy swap out a local filesystem for a remote one.
- Have a generic API for handling common tasks across multiple file storage engines.
- Have consistent output which you can rely on.
- Integrate well with other packages/frameworks.
- Be cacheable.
- Emulate directories in systems that support non, like AwsS3.
Trough Composer, obviously:
{
"require": {
"frenkynet/flysystem": "0.1.*"
}
}- Local
- Amazon Web Services - S3
- Dropbox
- Ftp
- Sftp (through phpseclib)
- Zip (through ZipArchive)
- Azure
- PR's welcome?
- Memory (array caching)
- Redis (through Predis)
use Flysystem\Filesystem;
use Flysystem\Adapter\Local as Adapter;
$filesystem = new Filesystem(new Adapter(__DIR__.'/path/to/root'));use Flysystem\Filesystem;
use Flysystem\Adapter\Zip as Adapter;
$filesystem = new Filesystem(new Adapter(__DIR__.'/path/to/archive.zip'));use Aws\S3\S3Client;
use Flysystem\Filesystem;
use Flysystem\Adapter\AwsS3 as Adapter;
$client = S3Client::factory(array(
'key' => '[your key]',
'secret' => '[your secret]',
));
$filesystem = new Filesystem(new Adapter($client, 'bucket-name', 'optional-prefix'));use Dropbox\Client;
use Flysystem\Filesystem;
use Flysystem\Adapter\Dropbox as Adapter;
$client = new Client($token, $appName);
$filesystem = new Filesystem(new Adapter($client), 'optional/path/prefix');use Flysystem\Filesystem;
use Flysystem\Adapter\Ftp as Adapter;
$filesystem = new Filesystem(new Adapter(array(
'host' => 'ftp.example.com',
'port' => 21,
'username' => 'username',
'password' => 'password',
'root' => '/path/to/root',
'passive' => true,
'ssl' => true,
'timeout' => 30,
)));use Flysystem\Filesystem;
use Flysystem\Adapter\Sftp as Adapter;
$filesystem = new Filesystem(new Adapter(array(
'host' => 'example.com',
'port' => 21,
'username' => 'username',
'password' => 'password',
'privateKey' => 'path/to/or/contents/of/privatekey'
'root' => '/path/to/root',
'timeout' => 10,
)));use Flysystem\Filesystem;
use Flysystem\Adapter\Local as Adapter;
use Flysystem\Cache\Predis as Cache;
$filesystem = new Filesystem(new Adapter(__DIR__.'/path/to/root'), new Cache);
// Or supply a client
$client = new Predis\Client;
$filesystem = new Filesystem(new Adapter(__DIR__.'/path/to/root'), new Cache($client));Write Files
$filemanager->write('filename.txt', 'contents');Read Files
$contents = $filemanager->read('filename.txt');Update Files
$filemanager->update('filename.txt', 'new contents');Delete Files
$filemanager->delete('filename.txt');Rename Files
$filemanager->rename('filename.txt', 'newname.txt');Get Mimetypes
$mimetype = $filemanager->getMimetype('filename.txt');Get Timestamps
$timestamp = $filemanager->getTimestamp('filename.txt');Get File Sizes
$size = $filemanager->getSize('filename.txt');Create Directories
$filemanager->createDir('nested/directory');Directories are also made implicitly when writing to a deeper path
$filemanager->write('path/to/filename.txt', 'contents');Delete Directories
$filemanager->deleteDir('path/to/directory');Manage Visibility
Visibility is the abstraction of file permissions across multiple platforms. Visibility can be either public or private.
use Flysystem\AdapterInterface;
$filesystem->write('db.backup', $backup, AdapterInterface::VISIBILITY_PRIVATE);
// or simply
$filesystem->write('db.backup', $backup, 'private');You can also change and check visibility of existing files
if ($filesystem->getVisibility('secret.txt') === 'private') {
$filesystem->setVisibility('secret.txt', 'public');
}$contents = $filemanager->listContents();The result of a contents listing is a collection of arrays containing all the metadata the file manager knows at that time. By default a you'll receive path info and file type. Additional info could be supplied by default depending on the adapter used.
Example:
foreach ($contents as $object) {
echo $object['base name'].' is located at'.$object['path'].' and is a '.$object['type'];
}$paths = $filemanager->listPaths();
foreach ($paths as $path) {
echo $path;
}$listing = $filesystem->listWith('mimetype', 'size', 'timestamp');
foreach ($listing as $object) {
echo $object['path'].' has mimetype: '.$object['mimetype'];
}

