1. Go to this page and download the library: Download diecoding/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/ */
diecoding / yii2-flysystem example snippets
return [
// ...
'components' => [
// ...
'fs' => [
'class' => \diecoding\flysystem\LocalComponent::class,
'path' => dirname(dirname(__DIR__)) . '/storage', // or you can use @alias
'secret' => 'my-secret', // for secure route url
// 'action' => '/site/file', // action route
// 'prefix' => '',
],
],
];
return [
// ...
'components' => [
'fs' => [
'class' => \diecoding\flysystem\SftpComponent::class,
'host' => 'hostname',
'username' => 'username',
'password' => null, // password (optional, default: null) set to null if privateKey is used
// 'privateKey' => '/path/to/my/private_key', // private key (optional, default: null) can be used instead of password, set to null if password is set
// 'passphrase' => 'super-secret-password', // passphrase (optional, default: null), set to null if privateKey is not used or has no passphrase
// 'port' => 22,
// 'useAgent' => true,
// 'timeout' => 10,
// 'maxTries' => 4,
// 'hostFingerprint' => null,
// 'connectivityChecker' => null, // connectivity checker (must be an implementation of `League\Flysystem\PhpseclibV2\ConnectivityChecker` to check if a connection can be established (optional, omit if you don't need some special handling for setting reliable connections)
// 'preferredAlgorithms' => [],
// 'root' => '/root/path/', // or you can use @alias
// 'action' => '/site/file', // action route
// 'prefix' => '',
],
],
];
if (Yii::$app->fs->visibility('filename.ext') === \League\Flysystem\Visibility::PRIVATE) {
Yii::$app->fs->setVisibility('filename.ext', \League\Flysystem\Visibility::PUBLIC);
}
$contents = Yii::$app->fs->listContents();
foreach ($contents as $object) {
echo $object['basename']
. ' is located at' . $object['path']
. ' and is a ' . $object['type'];
}
/**
* @property string|null $file
*/
class Model extends \yii\db\ActiveRecord
{
use \diecoding\flysystem\traits\ModelTrait;
// ...
public function rules()
{
return [
['image', 'string'], // Stores the filename
];
}
/**
* @inheritdoc
*/
protected function attributePaths()
{
return [
'image' => 'images/'
];
}
// ...
}
$image = \yii\web\UploadedFile::getInstance($model, 'image');
// Save image_thumb.* to Flysystem (fs) on //my_bucket/images/ path
// The extension of the file will be determined by the submitted file type
// This allows multiple file types upload (png,jpg,gif,...)
// $model->image will hold "image_thumb.png" after this call finish with success
$model->saveUploadedFile($image, 'image', 'image_thumb');
$model->save();
// Save image_thumb.png to Flysystem (fs) on //my_bucket/images/ path
// The extension of the file will be determined by the submitted file type
// This force the extension to *.png
$model->saveUploadedFile($image, 'image', 'image_thumb.png', false);
$model->save();
// Remove the file with named saved on the image attribute
// Continuing the example, here "//my_bucket/images/my_image.png" will be deleted from Flysystem (fs)
$model->removeFile('image');
$model->save();
// Get the URL to the image on Flysystem (fs)
$model->getFileUrl('image');
// Get the presigned URL to the image on Flysystem (fs)
// The default duration is "+5 Minutes"
$model->getFilePresignedUrl('image');
public function getFsComponent()
{
return Yii::$app->get('my_fs_component');
}
protected function attributePaths()
{
return [
'logo' => 'logos/',
'badge' => 'images/badges/'
];
}
// or use another attribute, example: id
// ! Note: id must contain a value first if you don't want it to be empty
protected function attributePaths()
{
return [
'logo' => 'thumbnail/' . $this->id . '/logos/',
'badge' => 'thumbnail/' . $this->id . '/images/badges/'
];
}
protected function getPresignedUrlDuration($attribute)
{
return new \DateTimeImmutable('+2 Hours');
}
// or if you want to set the attribute differently
protected function getPresignedUrlDuration($attribute)
{
switch ($attribute) {
case 'badge':
return new \DateTimeImmutable('+2 Hours');
break;
default:
return new \DateTimeImmutable('+1 Days');
break;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.