PHP code example of creocoder / yii2-flysystem
1. Go to this page and download the library: Download creocoder/yii2-flysystem library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
creocoder / yii2-flysystem example snippets
return [
//...
'components' => [
//...
'fs' => [
'class' => \creocoder\flysystem\LocalFilesystem::class,
'path' => '@webroot/files',
],
],
];
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,
],
],
];
return [
//...
'components' => [
//...
'memoryFs' => [
'class' => \creocoder\flysystem\InMemoryFilesystem::class,
],
],
];
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'
],
],
];
return [
//...
'components' => [
//...
'azureFs' => [
'class' => \creocoder\flysystem\AzureFilesystem::class,
'accountName' => 'your-account-name',
'accountKey' => 'your-account-key',
'container' => 'your-container',
// 'prefix' => 'your-prefix',
],
],
];
return [
//...
'components' => [
//...
'dropboxFs' => [
'class' => \creocoder\flysystem\DropboxFilesystem::class,
'token' => 'your-token',
// 'prefix' => 'your-prefix',
],
],
];
return [
//...
'components' => [
//...
'googleCloudFs' => [
'class' => \creocoder\flysystem\GoogleCloudFilesystem::class,
'projectId' => 'GOOGLE_PROJECT_ID',
'bucket' => 'GOOGLE_BUCKET',
'keyFilePath' => '@app/config/google-credentials.json',
// 'prefix' => 'your-prefix',
],
],
];
return [
//...
'components' => [
//...
'gridFs' => [
'class' => \creocoder\flysystem\GridFSFilesystem::class,
'uri' => 'mongodb://localhost:27017',
'database' => 'your-database',
// 'bucketName' => 'fs',
],
],
];
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',
],
],
];
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',
],
],
];
return [
//...
'components' => [
//...
'ziparchiveFs' => [
'class' => \creocoder\flysystem\ZipArchiveFilesystem::class,
'path' => '@webroot/files/archive.zip',
// 'prefix' => 'your-prefix',
],
],
];
use League\Flysystem\Visibility;
return [
//...
'components' => [
//...
'fsID' => [
//...
'config' => [
'visibility' => Visibility::PRIVATE,
],
],
],
];
Yii::$app->fs->write('filename.ext', 'contents');
$stream = fopen('/path/to/somefile.ext', 'r+');
Yii::$app->fs->writeStream('filename.ext', $stream);
$contents = Yii::$app->fs->read('filename.ext');
$stream = Yii::$app->fs->readStream('filename.ext');
$contents = stream_get_contents($stream);
fclose($stream);
$exists = Yii::$app->fs->fileExists('filename.ext');
$exists = Yii::$app->fs->directoryExists('path/to/directory');
Yii::$app->fs->delete('filename.ext');
Yii::$app->fs->move('filename.ext', 'newname.ext');
Yii::$app->fs->copy('filename.ext', 'copy.ext');
$mimetype = Yii::$app->fs->mimeType('filename.ext');
$timestamp = Yii::$app->fs->lastModified('filename.ext');
$size = Yii::$app->fs->fileSize('filename.ext');
Yii::$app->fs->createDirectory('path/to/directory');
Yii::$app->fs->write('path/to/filename.ext', 'contents');
Yii::$app->fs->deleteDirectory('path/to/directory');
use League\Flysystem\Visibility;
Yii::$app->fs->write('filename.ext', 'contents', [
'visibility' => Visibility::PRIVATE
]);
use League\Flysystem\Visibility;
if (Yii::$app->fs->visibility('filename.ext') === Visibility::PRIVATE) {
Yii::$app->fs->setVisibility('filename.ext', Visibility::PUBLIC);
}
$contents = Yii::$app->fs->listContents('', false);
foreach ($contents as $item) {
echo $item->path();
echo $item->isFile() ? 'file' : 'directory';
}
$contents = Yii::$app->fs->listContents('path/to/directory', true);
// Old (v1)
use League\Flysystem\AdapterInterface;
AdapterInterface::VISIBILITY_PRIVATE;
AdapterInterface::VISIBILITY_PUBLIC;
// New (v3)
use League\Flysystem\Visibility;
Visibility::PRIVATE;
Visibility::PUBLIC;