PHP code example of weison-tech / yii2-file-kit
1. Go to this page and download the library: Download weison-tech/yii2-file-kit 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/ */
weison-tech / yii2-file-kit example snippets
'fileStorage'=>[
'class' => 'trntv\filekit\Storage',
'baseUrl' => '@web/uploads'
'filesystem'=> ...
// OR
'filesystemComponent' => ...
],
'fileStorage'=>[
...
'filesystem'=> function() {
$adapter = new \League\Flysystem\Adapter\Local('some/path/to/storage');
return new League\Flysystem\Filesystem($adapter);
}
]
'fileStorage'=>[
...
'filesystem'=> [
'class' => 'app\components\FilesystemBuilder',
'path' => '@webroot/uploads'
...
]
]
'components' => [
...
'fs' => [
'class' => 'creocoder\flysystem\LocalFilesystem',
'path' => '@webroot/files'
],
...
]
'components' => [
...
'fileStorage'=>[
'filesystemComponent'=> 'fs'
],
...
]
public function actions(){
return [
'upload'=>[
'class'=>'trntv\filekit\actions\UploadAction',
//'deleteRoute' => 'my-custom-delete', // my custom delete action for deleting just uploaded files(not yet saved)
//'fileStorage' => 'myfileStorage', // my custom fileStorage from configuration
'multiple' => true,
'disableCsrf' => true,
'responseFormat' => Response::FORMAT_JSON,
'responsePathParam' => 'path',
'responseBaseUrlParam' => 'base_url',
'responseUrlParam' => 'url',
'responseDeleteUrlParam' => 'delete_url',
'responseMimeTypeParam' => 'type',
'responseNameParam' => 'name',
'responseSizeParam' => 'size',
'deleteRoute' => 'delete',
'fileStorage' => 'fileStorage', // Yii::$app->get('fileStorage')
'fileStorageParam' => 'fileStorage', // ?fileStorage=someStorageComponent
'sessionKey' => '_uploadedFiles',
'allowChangeFilestorage' => false,
'validationRules' => [
...
],
'on afterSave' => function($event) {
/* @var $file \League\Flysystem\File */
$file = $event->file
// do something (resize, add watermark etc)
}
]
];
}
public function actions(){
return [
'delete'=>[
'class'=>'trntv\filekit\actions\DeleteAction',
//'fileStorage' => 'fileStorageMy', // my custom fileStorage from configuration(such as in the upload action)
]
];
}
public function actions(){
return [
'view'=>[
'class'=>'trntv\filekit\actions\ViewAction',
]
];
}
echo \trntv\filekit\widget\Upload::widget([
'model' => $model,
'attribute' => 'files',
'url' => ['upload'],
'sortable' => true,
'maxFileSize' => 10 * 1024 * 1024, // 10Mb
'minFileSize' => 1 * 1024 * 1024, // 1Mb
'maxNumberOfFiles' => 3 // default 1,
'acceptFileTypes' => new JsExpression('/(\.|\/)(gif|jpe?g|png)$/i'),
'showPreviewFilename' => false,
'clientOptions' => [ ...other blueimp options... ]
]);
echo $form->field($model, 'files')->widget(
'\trntv\filekit\widget\Upload',
[
'url' => ['upload'],
'sortable' => true,
'maxFileSize' => 10 * 1024 * 1024, // 10 MiB
'maxNumberOfFiles' => 3,
'clientOptions' => [ ...other blueimp options... ]
]
);
'clientOptions' => [
'start' => new JsExpression('function(e, data) { ... do something ... }'),
'done' => new JsExpression('function(e, data) { ... do something ... }'),
'fail' => new JsExpression('function(e, data) { ... do something ... }'),
'always' => new JsExpression('function(e, data) { ... do something ... }'),
]
public function behaviors()
{
return [
'file' => [
'class' => 'trntv\filekit\behaviors\UploadBehavior',
'filesStorage' => 'myfileStorage', // my custom fileStorage from configuration(for properly remove the file from disk)
'multiple' => true,
'attribute' => 'files',
'uploadRelation' => 'uploadedFiles',
'pathAttribute' => 'path',
'baseUrlAttribute' => 'base_url',
'typeAttribute' => 'type',
'sizeAttribute' => 'size',
'nameAttribute' => 'name',
'orderAttribute' => 'order'
],
];
}
public function behaviors()
{
return [
'file' => [
'class' => 'trntv\filekit\behaviors\UploadBehavior',
'filesStorage' => 'fileStorageMy', // my custom fileStorage from configuration(for properly remove the file from disk)
'attribute' => 'file',
'pathAttribute' => 'path',
'baseUrlAttribute' => 'base_url',
...
],
];
}
php composer.phar
public function actions(){
return [
'upload'=>[
'class'=>'trntv\filekit\actions\UploadAction',
...
'on afterSave' => function($event) {
/* @var $file \League\Flysystem\File */
$file = $event->file;
// create new Intervention Image
$img = Intervention\Image\ImageManager::make($file->read());
// insert watermark at bottom-right corner with 10px offset
$img->insert('public/watermark.png', 'bottom-right', 10, 10);
// save image
$file->put($img->encode());
}
...
]
];
}