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.
The preferred way to install this extension is through composer.
Either run
$ composer require creocoder/yii2-flysystemor add
"creocoder/yii2-flysystem": "0.8.*"
to the require section of your composer.json file.
Configure application components as follows
return [
    //...
    'components' => [
        //...
        'fs' => [
            'class' => 'creocoder\flysystem\LocalFilesystem',
            'path' => '@webroot/files',
            // 'writeFlags' => LOCK_EX,
            // 'linkHandling' => 0002,
            // 'permissions' => [],
        ],
    ],
];Configure application components as follows
return [
    //...
    'components' => [
        //...
        'ftpFs' => [
            'class' => 'creocoder\flysystem\FtpFilesystem',
            'host' => 'ftp.example.com',
            // 'port' => 21,
            // 'username' => 'your-username',
            // 'password' => 'your-password',
            // 'ssl' => true,
            // 'timeout' => 60,
            // 'root' => '/path/to/root',
            // 'permPrivate' => 0700,
            // 'permPublic' => 0744,
            // 'passive' => false,
            // 'transferMode' => FTP_TEXT,
        ],
    ],
];Configure application components as follows
return [
    //...
    'components' => [
        //...
        'nullFs' => [
            'class' => 'creocoder\flysystem\NullFilesystem',
        ],
    ],
];Either run
$ composer require league/flysystem-aws-s3-v3or add
"league/flysystem-aws-s3-v3": "~1.0"
to the require section of your composer.json file and configure application components as follows
return [
    //...
    'components' => [
        //...
        'awss3Fs' => [
            'class' => 'creocoder\flysystem\AwsS3Filesystem',
            'key' => 'your-key',
            'secret' => 'your-secret',
            'bucket' => 'your-bucket',
            'region' => 'your-region',
            // 'version' => 'latest',
            // 'baseUrl' => 'your-base-url',
            // 'prefix' => 'your-prefix',
            // 'options' => [],
            // 'endpoint' => 'http://my-custom-url'
        ],
    ],
];Add the following to the repositories section of your composer.json
{
    "type": "pear",
    "url": "http://pear.php.net"
}
Either run
$ composer require league/flysystem-azureor add
"league/flysystem-azure": "~1.0"
to the require section of your composer.json file and configure application components as follows
return [
    //...
    'components' => [
        //...
        'azureFs' => [
            'class' => 'creocoder\flysystem\AzureFilesystem',
            'accountName' => 'your-account-name',
            'accountKey' => 'your-account-key',
            'container' => 'your-container',
        ],
    ],
];Either run
$ composer require league/flysystem-copyor add
"league/flysystem-copy": "~1.0"
to the require section of your composer.json file and configure application components as follows
return [
    //...
    'components' => [
        //...
        'copyFs' => [
            'class' => 'creocoder\flysystem\CopyFilesystem',
            'consumerKey' => 'your-consumer-key',
            'consumerSecret' => 'your-consumer-secret',
            'accessToken' => 'your-access-token',
            'tokenSecret' => 'your-token-secret',
            // 'prefix' => 'your-prefix',
        ],
    ],
];Either run
$ composer require league/flysystem-dropboxor add
"league/flysystem-dropbox": "~1.0"
to the require section of your composer.json file and configure application components as follows
return [
    //...
    'components' => [
        //...
        'dropboxFs' => [
            'class' => 'creocoder\flysystem\DropboxFilesystem',
            'token' => 'your-token',
            'app' => 'your-app',
            // 'prefix' => 'your-prefix',
        ],
    ],
];Run
$ composer require "superbalist/flysystem-google-storage": "^5.0"and configure application components as follows
return [
    //...
    'components' => [
        //...
        'googleCloudFs' => [
            'class' => 'creocoder\flysystem\GoogleCloudFilesystem',
            'projectId' => 'GOOGLE_PROJECT_ID',
            'bucket' => 'GOOGLE_BUCKET',
            'keyFilePath' => 'GOOGLE_KEY_FILE_PATH',
        ],
    ],
];Note: Credential configuration is read from the keyFile.
Either run
$ composer require league/flysystem-gridfsor add
"league/flysystem-gridfs": "~1.0"
to the require section of your composer.json file and configure application components as follows
return [
    //...
    'components' => [
        //...
        'gridFs' => [
            'class' => 'creocoder\flysystem\GridFSFilesystem',
            'server' => 'mongodb://localhost:27017',
            'database' => 'your-database',
        ],
    ],
];Either run
$ composer require league/flysystem-rackspaceor add
"league/flysystem-rackspace": "~1.0"
to the require section of your composer.json file and configure application components as follows
return [
    //...
    'components' => [
        //...
        'rackspaceFs' => [
            'class' => 'creocoder\flysystem\RackspaceFilesystem',
            'endpoint' => 'your-endpoint',
            'region' => 'your-region',
            'username' => 'your-username',
            'apiKey' => 'your-api-key',
            'container' => 'your-container',
            // 'prefix' => 'your-prefix',
        ],
    ],
];Either run
$ composer require league/flysystem-sftpor add
"league/flysystem-sftp": "~1.0"
to the require section of your composer.json file and configure application components as follows
return [
    //...
    'components' => [
        //...
        'sftpFs' => [
            'class' => 'creocoder\flysystem\SftpFilesystem',
            'host' => 'sftp.example.com',
            // 'port' => 22,
            'username' => 'your-username',
            'password' => 'your-password',
            'privateKey' => '/path/to/or/contents/of/privatekey',
            // 'timeout' => 60,
            // 'root' => '/path/to/root',
            // 'permPrivate' => 0700,
            // 'permPublic' => 0744,
        ],
    ],
];Either run
$ composer require league/flysystem-webdavor add
"league/flysystem-webdav": "~1.0"
to the require section of your composer.json file and configure application components as follows
return [
    //...
    'components' => [
        //...
        'webdavFs' => [
            'class' => 'creocoder\flysystem\WebDAVFilesystem',
            'baseUri' => 'your-base-uri',
            // 'userName' => 'your-user-name',
            // 'password' => 'your-password',
            // 'proxy' => 'your-proxy',
            // 'authType' => \Sabre\DAV\Client::AUTH_BASIC,
            // 'encoding' => \Sabre\DAV\Client::ENCODING_IDENTITY,
            // 'prefix' => 'your-prefix',
        ],
    ],
];Either run
$ composer require league/flysystem-ziparchiveor add
"league/flysystem-ziparchive": "~1.0"
to the require section of your composer.json file and configure application components as follows
return [
    //...
    'components' => [
        //...
        'ziparchiveFs' => [
            'class' => 'creocoder\flysystem\ZipArchiveFilesystem',
            'path' => '@webroot/files/archive.zip',
            // 'prefix' => 'your-prefix',
        ],
    ],
];Either run
$ composer require league/flysystem-cached-adapteror add
"league/flysystem-cached-adapter": "~1.0"
to the require section of your composer.json file and configure fsID application component as follows
return [
    //...
    'components' => [
        //...
        'fsID' => [
            //...
            'cache' => 'cacheID',
            // 'cacheKey' => 'flysystem',
            // 'cacheDuration' => 3600,
        ],
    ],
];Either run
$ composer require league/flysystem-replicate-adapteror add
"league/flysystem-replicate-adapter": "~1.0"
to the require section of your composer.json file and configure fsID application component as follows
return [
    //...
    'components' => [
        //...
        'fsID' => [
            //...
            'replica' => 'anotherFsID',
        ],
    ],
];Configure fsID application component as follows
return [
    //...
    'components' => [
        //...
        'fsID' => [
            //...
            'config' => [
                'visibility' => \League\Flysystem\AdapterInterface::VISIBILITY_PRIVATE,
            ],
        ],
    ],
];To write file
Yii::$app->fs->write('filename.ext', 'contents');To write file using stream contents
$stream = fopen('/path/to/somefile.ext', 'r+');
Yii::$app->fs->writeStream('filename.ext', $stream);To update file
Yii::$app->fs->update('filename.ext', 'contents');To update file using stream contents
$stream = fopen('/path/to/somefile.ext', 'r+');
Yii::$app->fs->updateStream('filename.ext', $stream);To write or update file
Yii::$app->fs->put('filename.ext', 'contents');To write or update file using stream contents
$stream = fopen('/path/to/somefile.ext', 'r+');
Yii::$app->fs->putStream('filename.ext', $stream);To read 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->has('filename.ext');To delete file
Yii::$app->fs->delete('filename.ext');To read and delete file
$contents = Yii::$app->fs->readAndDelete('filename.ext');To rename file
Yii::$app->fs->rename('filename.ext', 'newname.ext');To get file mimetype
$mimetype = Yii::$app->fs->getMimetype('filename.ext');To get file timestamp
$timestamp = Yii::$app->fs->getTimestamp('filename.ext');To get file size
$timestamp = Yii::$app->fs->getSize('filename.ext');To create directory
Yii::$app->fs->createDir('path/to/directory');Directories are also made implicitly when writing to a deeper path
Yii::$app->fs->write('path/to/filename.ext');To delete directory
Yii::$app->fs->deleteDir('path/to/filename.ext');Visibility is the abstraction of file permissions across multiple platforms. Visibility can be either public or private.
use League\Flysystem\AdapterInterface;
Yii::$app->fs->write('filename.ext', 'contents', [
    'visibility' => AdapterInterface::VISIBILITY_PRIVATE
]);You can also change and check visibility of existing files
use League\Flysystem\AdapterInterface;
if (Yii::$app->fs->getVisibility('filename.ext') === AdapterInterface::VISIBILITY_PRIVATE) {
    Yii::$app->fs->setVisibility('filename.ext', AdapterInterface::VISIBILITY_PUBLIC);
}To list contents
$contents = Yii::$app->fs->listContents();
foreach ($contents as $object) {
    echo $object['basename']
        . ' is located at' . $object['path']
        . ' and is a ' . $object['type'];
}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);To list paths
$paths = Yii::$app->fs->listPaths();
foreach ($paths as $path) {
    echo $path;
}To list with ensured presence of specific metadata
$listing = Yii::$app->fs->listWith(
    ['mimetype', 'size', 'timestamp'],
    'optional/path/to/directory',
    true
);
foreach ($listing as $object) {
    echo $object['path'] . ' has mimetype: ' . $object['mimetype'];
}To get file info with explicit metadata
$info = Yii::$app->fs->getWithMetadata('path/to/filename.ext', ['timestamp', 'mimetype']);
echo $info['mimetype'];
echo $info['timestamp'];Support this project and others by creocoder via gratipay.