This extension provides Flysystem integration for the Yii framework. Flysystem is a filesystem abstraction which allows you to easily swap out a local filesystem for a remote one.
This version requires Flysystem 3.x and PHP 8.1+
The preferred way to install this extension is through composer.
Either run
$ composer require creocoder/yii2-flysystemor add
"creocoder/yii2-flysystem": "^2.0"
to the require section of your composer.json file.
Configure application components as follows
return [
//...
'components' => [
//...
'fs' => [
'class' => \creocoder\flysystem\LocalFilesystem::class,
'path' => '@webroot/files',
],
],
];Either run
$ composer require league/flysystem-ftpor add
"league/flysystem-ftp": "^3.0"
to the require section of your composer.json file and configure application components as follows
return [
//...
'components' => [
//...
'ftpFs' => [
'class' => \creocoder\flysystem\FtpFilesystem::class,
'host' => 'ftp.example.com',
// 'port' => 21,
// 'username' => 'your-username',
// 'password' => 'your-password',
// 'ssl' => true,
// 'timeout' => 90,
// 'root' => '/path/to/root',
// 'passive' => true,
],
],
];Configure application components as follows
return [
//...
'components' => [
//...
'memoryFs' => [
'class' => \creocoder\flysystem\InMemoryFilesystem::class,
],
],
];Either run
$ composer require league/flysystem-aws-s3-v3or add
"league/flysystem-aws-s3-v3": "^3.0"
to the require section of your composer.json file and configure application components as follows
return [
//...
'components' => [
//...
'awss3Fs' => [
'class' => \creocoder\flysystem\AwsS3Filesystem::class,
'key' => 'your-key',
'secret' => 'your-secret',
'bucket' => 'your-bucket',
'region' => 'your-region',
// 'version' => 'latest',
// 'prefix' => 'your-prefix',
// 'endpoint' => 'http://my-custom-url'
],
],
];Either run
$ composer require league/flysystem-azure-blob-storageor add
"league/flysystem-azure-blob-storage": "^3.0"
to the require section of your composer.json file and configure application components as follows
return [
//...
'components' => [
//...
'azureFs' => [
'class' => \creocoder\flysystem\AzureFilesystem::class,
'accountName' => 'your-account-name',
'accountKey' => 'your-account-key',
'container' => 'your-container',
// 'prefix' => 'your-prefix',
],
],
];Either run
$ composer require spatie/flysystem-dropboxor add
"spatie/flysystem-dropbox": "^3.0"
to the require section of your composer.json file and configure application components as follows
return [
//...
'components' => [
//...
'dropboxFs' => [
'class' => \creocoder\flysystem\DropboxFilesystem::class,
'token' => 'your-token',
// 'prefix' => 'your-prefix',
],
],
];Run
$ composer require league/flysystem-google-cloud-storageand configure application components as follows
return [
//...
'components' => [
//...
'googleCloudFs' => [
'class' => \creocoder\flysystem\GoogleCloudFilesystem::class,
'projectId' => 'GOOGLE_PROJECT_ID',
'bucket' => 'GOOGLE_BUCKET',
'keyFilePath' => '@app/config/google-credentials.json',
// 'prefix' => 'your-prefix',
],
],
];Note: Credential configuration is read from the keyFilePath.
Either run
$ composer require league/flysystem-gridfsor add
"league/flysystem-gridfs": "^3.0"
to the require section of your composer.json file and configure application components as follows
return [
//...
'components' => [
//...
'gridFs' => [
'class' => \creocoder\flysystem\GridFSFilesystem::class,
'uri' => 'mongodb://localhost:27017',
'database' => 'your-database',
// 'bucketName' => 'fs',
],
],
];Either run
$ composer require league/flysystem-sftp-v3or add
"league/flysystem-sftp-v3": "^3.0"
to the require section of your composer.json file and configure application components as follows
return [
//...
'components' => [
//...
'sftpFs' => [
'class' => \creocoder\flysystem\SftpFilesystem::class,
'host' => 'sftp.example.com',
// 'port' => 22,
'username' => 'your-username',
'password' => 'your-password',
// 'privateKey' => '/path/to/or/contents/of/privatekey',
// 'passphrase' => 'your-passphrase',
// 'timeout' => 10,
// 'root' => '/path/to/root',
],
],
];Either run
$ composer require league/flysystem-webdavor add
"league/flysystem-webdav": "^3.0"
to the require section of your composer.json file and configure application components as follows
return [
//...
'components' => [
//...
'webdavFs' => [
'class' => \creocoder\flysystem\WebDAVFilesystem::class,
'baseUri' => 'your-base-uri',
// 'userName' => 'your-user-name',
// 'password' => 'your-password',
// 'proxy' => 'your-proxy',
// 'prefix' => 'your-prefix',
],
],
];Either run
$ composer require league/flysystem-ziparchiveor add
"league/flysystem-ziparchive": "^3.0"
to the require section of your composer.json file and configure application components as follows
return [
//...
'components' => [
//...
'ziparchiveFs' => [
'class' => \creocoder\flysystem\ZipArchiveFilesystem::class,
'path' => '@webroot/files/archive.zip',
// 'prefix' => 'your-prefix',
],
],
];Configure fsID application component as follows
use League\Flysystem\Visibility;
return [
//...
'components' => [
//...
'fsID' => [
//...
'config' => [
'visibility' => Visibility::PRIVATE,
],
],
],
];To write a file
Yii::$app->fs->write('filename.ext', 'contents');To write a file using stream contents
$stream = fopen('/path/to/somefile.ext', 'r+');
Yii::$app->fs->writeStream('filename.ext', $stream);To read a file
$contents = Yii::$app->fs->read('filename.ext');To retrieve a read-stream
$stream = Yii::$app->fs->readStream('filename.ext');
$contents = stream_get_contents($stream);
fclose($stream);To check if a file exists
$exists = Yii::$app->fs->fileExists('filename.ext');To check if a directory exists
$exists = Yii::$app->fs->directoryExists('path/to/directory');To delete a file
Yii::$app->fs->delete('filename.ext');To move a file
Yii::$app->fs->move('filename.ext', 'newname.ext');To copy a file
Yii::$app->fs->copy('filename.ext', 'copy.ext');To get a file mimetype
$mimetype = Yii::$app->fs->mimeType('filename.ext');To get a file last modified timestamp
$timestamp = Yii::$app->fs->lastModified('filename.ext');To get a file size
$size = Yii::$app->fs->fileSize('filename.ext');To create a directory
Yii::$app->fs->createDirectory('path/to/directory');Directories are also made implicitly when writing to a deeper path
Yii::$app->fs->write('path/to/filename.ext', 'contents');To delete a directory
Yii::$app->fs->deleteDirectory('path/to/directory');Visibility is the abstraction of file permissions across multiple platforms. Visibility can be either public or private.
use League\Flysystem\Visibility;
Yii::$app->fs->write('filename.ext', 'contents', [
'visibility' => Visibility::PRIVATE
]);You can also change and check visibility of existing files
use League\Flysystem\Visibility;
if (Yii::$app->fs->visibility('filename.ext') === Visibility::PRIVATE) {
Yii::$app->fs->setVisibility('filename.ext', Visibility::PUBLIC);
}To list contents
$contents = Yii::$app->fs->listContents('', false);
foreach ($contents as $item) {
echo $item->path();
echo $item->isFile() ? 'file' : 'directory';
}By default Flysystem lists the top directory non-recursively. You can supply a directory name and recursive boolean to get more precise results
$contents = Yii::$app->fs->listContents('path/to/directory', true);If you are upgrading from the Flysystem v1 version of this extension, please note the following breaking changes:
- Caching: The cached adapter is no longer available in Flysystem v3
- Replication: The replicate adapter is no longer available in Flysystem v3
- Rackspace: The Rackspace adapter has been removed as the service is discontinued
- NullFilesystem: Replaced with
InMemoryFilesystem
| v1 Method | v3 Method |
|---|---|
has($path) |
fileExists($path) / directoryExists($path) |
createDir($path) |
createDirectory($path) |
deleteDir($path) |
deleteDirectory($path) |
rename($path, $newpath) |
move($source, $destination) |
getTimestamp($path) |
lastModified($path) |
getMimetype($path) |
mimeType($path) |
getSize($path) |
fileSize($path) |
getVisibility($path) |
visibility($path) |
update() / put() |
write() |
updateStream() / putStream() |
writeStream() |
// Old (v1)
use League\Flysystem\AdapterInterface;
AdapterInterface::VISIBILITY_PRIVATE;
AdapterInterface::VISIBILITY_PUBLIC;
// New (v3)
use League\Flysystem\Visibility;
Visibility::PRIVATE;
Visibility::PUBLIC;Support this project and others by creocoder via gratipay.