{"id":16314242,"url":"https://github.com/rustamwin/yii2-filekit","last_synced_at":"2026-04-28T18:03:24.384Z","repository":{"id":62538369,"uuid":"106660028","full_name":"rustamwin/yii2-filekit","owner":"rustamwin","description":null,"archived":false,"fork":false,"pushed_at":"2017-10-14T08:06:48.000Z","size":29,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-16T16:35:34.107Z","etag":null,"topics":["blueimp","filesystem","upload-widget","yii2","yii2-extension"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rustamwin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-10-12T07:33:11.000Z","updated_at":"2017-10-26T05:19:00.000Z","dependencies_parsed_at":"2022-11-02T15:30:26.934Z","dependency_job_id":null,"html_url":"https://github.com/rustamwin/yii2-filekit","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rustamwin%2Fyii2-filekit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rustamwin%2Fyii2-filekit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rustamwin%2Fyii2-filekit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rustamwin%2Fyii2-filekit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rustamwin","download_url":"https://codeload.github.com/rustamwin/yii2-filekit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250955223,"owners_count":21513490,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blueimp","filesystem","upload-widget","yii2","yii2-extension"],"created_at":"2024-10-10T21:53:29.004Z","updated_at":"2026-04-28T18:03:24.278Z","avatar_url":"https://github.com/rustamwin.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Packagist](https://img.shields.io/packagist/dt/rustamwin/yii2-filekit.svg)]()\n[![Dependency Status](https://www.versioneye.com/php/rustamwin:yii2-filekit/badge.svg)](https://www.versioneye.com/php/rustamwin:yii2-filekit/2.0.0)\n\nThis kit is designed to automate routine processes of uploading files, their saving and storage.\nIt includes:\n- File upload widget (based on [Blueimp File Upload](https://github.com/blueimp/jQuery-File-Upload))\n- Component for storing files (built on top of [flysystem](https://github.com/thephpleague/flysystem))\n- Actions to download, delete, and view (download) files\n- Behavior for saving files in the model and delete files when you delete a model\n\nHere you can see list of available [filesystem adapters](https://github.com/thephpleague/flysystem#adapters)\n\nDemo\n----\nSince file kit is a part of [yii2-starter-kit](https://github.com/rustamwin/yii2-starter-kit) it's demo can be found in starter kit demo [here](http://backend.yii2-starter-kit.terentev.net/article/create).\n\n# Installation\nThe preferred way to install this extension is through [composer](http://getcomposer.org/download/).\n\nEither run\n\n```\nphp composer.phar require rustamwin/yii2-filekit\n```\n\nor add\n\n```\n\"rustamwin/yii2-filekit\": \"@stable\"\n```\n\nto the require section of your `composer.json` file.\n\n# File Storage\nTo work with the File Kit you need to configure FileStorage first. This component is a layer of abstraction over the filesystem\n- Its main task to take on the generation of a unique name for each file and trigger corresponding events.\n```php\n'fileStorage'=\u003e[\n    'class' =\u003e 'rustamwin\\filekit\\Storage',\n    'baseUrl' =\u003e '@web/uploads'\n    'filesystem'=\u003e ...\n        // OR\n    'filesystemComponent' =\u003e ...    \n],\n```\nThere are several ways to configure `rustamwin\\filekit\\Storage` to work with `flysystem`.\n\n## Using Closure\n```php\n'fileStorage'=\u003e[\n    ...\n    'filesystem'=\u003e function() {\n        $adapter = new \\League\\Flysystem\\Adapter\\Local('some/path/to/storage');\n        return new League\\Flysystem\\Filesystem($adapter);\n    }\n]\n```\n## Using filesystem builder \n- Create a builder class that implements `rustamwin\\filekit\\filesystem\\FilesystemBuilderInterface` and implement method `build` which returns filesystem object. See ``examples/``\n- Add to your configuration:\n```php\n'fileStorage'=\u003e[\n    ...\n    'filesystem'=\u003e [\n        'class' =\u003e 'app\\components\\FilesystemBuilder',\n        'path' =\u003e '@webroot/uploads'\n        ...\n    ]\n]\n```\nRead more about flysystem at http://flysystem.thephpleague.com/\n\n## Using third-party extensions\n- Create filesystem component (example uses `creocoder/yii2-flysystem`)\n```php\n'components' =\u003e [\n    ...\n    'fs' =\u003e [\n        'class' =\u003e 'creocoder\\flysystem\\LocalFilesystem',\n        'path' =\u003e '@webroot/files'\n    ],\n    ...\n]\n```\n- Set filesystem component name in storage configuration:\n```php\n'components' =\u003e [\n    ...\n    'fileStorage'=\u003e[\n        'filesystemComponent'=\u003e 'fs'\n    ],\n    ...\n]\n```\n\n# Actions\nFile Kit contains several Actions to work with uploads.\n\n### Upload Action\nDesigned to save the file uploaded by the widget\n```php\npublic function actions(){\n    return [\n           'upload'=\u003e[\n               'class'=\u003e'rustamwin\\filekit\\actions\\UploadAction',\n               //'deleteRoute' =\u003e 'my-custom-delete', // my custom delete action for deleting just uploaded files(not yet saved)\n               //'fileStorage' =\u003e 'myfileStorage', // my custom fileStorage from configuration\n               'multiple' =\u003e true,\n               'disableCsrf' =\u003e true,\n               'responseFormat' =\u003e Response::FORMAT_JSON,\n               'responsePathParam' =\u003e 'path',\n               'responseBaseUrlParam' =\u003e 'base_url',\n               'responseUrlParam' =\u003e 'url',\n               'responseDeleteUrlParam' =\u003e 'delete_url',\n               'responseMimeTypeParam' =\u003e 'type',\n               'responseNameParam' =\u003e 'name',\n               'responseSizeParam' =\u003e 'size',\n               'deleteRoute' =\u003e 'delete',\n               'fileStorage' =\u003e 'fileStorage', // Yii::$app-\u003eget('fileStorage')\n               'fileStorageParam' =\u003e 'fileStorage', // ?fileStorage=someStorageComponent\n               'sessionKey' =\u003e '_uploadedFiles',\n               'allowChangeFilestorage' =\u003e false,\n               'validationRules' =\u003e [\n                    ...\n               ],\n               'on afterSave' =\u003e function($event) {\n                    /* @var $file \\League\\Flysystem\\File */\n                    $file = $event-\u003efile\n                    // do something (resize, add watermark etc)\n               }\n           ]\n       ];\n}\n```\nSee additional settings in the corresponding class\n\n### Delete Action\n```php\npublic function actions(){\n    return [\n       'delete'=\u003e[\n           'class'=\u003e'rustamwin\\filekit\\actions\\DeleteAction',\n           //'fileStorage' =\u003e 'fileStorageMy', // my custom fileStorage from configuration(such as in the upload action)\n       ]\n    ];\n}\n```\nSee additional settings in the corresponding class\n\n### View (Download) Action\n```php\npublic function actions(){\n    return [\n       'view'=\u003e[\n           'class'=\u003e'rustamwin\\filekit\\actions\\ViewAction',\n       ]\n    ];\n}\n```\nSee additional settings in the corresponding class\n\n# Upload Widget\nStandalone usage\n```php\necho \\rustamwin\\filekit\\widget\\Upload::widget([\n    'model' =\u003e $model,\n    'attribute' =\u003e 'files',\n    'url' =\u003e ['upload'],\n    'sortable' =\u003e true,\n    'maxFileSize' =\u003e 10 * 1024 * 1024, // 10Mb\n    'minFileSize' =\u003e 1 * 1024 * 1024, // 1Mb\n    'maxNumberOfFiles' =\u003e 3 // default 1,\n    'acceptFileTypes' =\u003e new JsExpression('/(\\.|\\/)(gif|jpe?g|png)$/i'),\n    'showPreviewFilename' =\u003e false,\n    'clientOptions' =\u003e [ ...other blueimp options... ]\n]);\n```\n\nWith ActiveForm\n```php\necho $form-\u003efield($model, 'files')-\u003ewidget(\n    '\\rustamwin\\filekit\\widget\\Upload',\n    [\n        'url' =\u003e ['upload'],\n        'sortable' =\u003e true,\n        'maxFileSize' =\u003e 10 * 1024 * 1024, // 10 MiB\n        'maxNumberOfFiles' =\u003e 3,\n        'clientOptions' =\u003e [ ...other blueimp options... ]\n    ]\n);\n```\n## Upload Widget events\nUpload widget trigger some of built-in blueimp events:\n- start\n- fail\n- done\n- always\n    \nYou can use them directly or add your custom handlers in options:\n```php\n'clientOptions' =\u003e [ \n    'start' =\u003e new JsExpression('function(e, data) { ... do something ... }'),\n    'done' =\u003e new JsExpression('function(e, data) { ... do something ... }'),\n    'fail' =\u003e new JsExpression('function(e, data) { ... do something ... }'),\n    'always' =\u003e new JsExpression('function(e, data) { ... do something ... }'),\n ]\n```\n\n# UploadBehavior\nThis behavior is designed to save uploaded files in the corresponding relation.\n\nSomewhere in model:\n\nFor multiple files\n```php\n public function behaviors()\n {\n    return [\n        'file' =\u003e [\n            'class' =\u003e 'rustamwin\\filekit\\behaviors\\UploadBehavior',\n            'filesStorage' =\u003e 'myfileStorage', // my custom fileStorage from configuration(for properly remove the file from disk)\n            'multiple' =\u003e true,\n            'attribute' =\u003e 'files',\n            'uploadRelation' =\u003e 'uploadedFiles',\n            'pathAttribute' =\u003e 'path',\n            'baseUrlAttribute' =\u003e 'base_url',\n            'typeAttribute' =\u003e 'type',\n            'sizeAttribute' =\u003e 'size',\n            'nameAttribute' =\u003e 'name',\n            'orderAttribute' =\u003e 'order'\n        ],\n    ];\n }\n```\n\nFor single file upload\n```php\n public function behaviors()\n {\n     return [\n          'file' =\u003e [\n              'class' =\u003e 'rustamwin\\filekit\\behaviors\\UploadBehavior',\n              'filesStorage' =\u003e 'fileStorageMy', // my custom fileStorage from configuration(for properly remove the file from disk)\n              'attribute' =\u003e 'file',\n              'pathAttribute' =\u003e 'path',\n              'baseUrlAttribute' =\u003e 'base_url',\n               ...\n          ],\n      ];\n }\n```\n\nSee additional settings in the corresponding class.\n\n# Validation\nThere are two ways you can perform validation over uploads.\nOn the client side validation is performed by Blueimp File Upload.\nHere is [documentation](https://github.com/blueimp/jQuery-File-Upload/wiki/Options#validation-options) about available options.\n\nOn the server side validation is performed by [[yii\\web\\UploadAction]], where you can configure validation rules for \n[[yii\\base\\DynamicModel]] that will be used in validation process\n\n# Tips\n## Adding watermark\nInstall ``intervention/image`` library\n```\ncomposer require intervention/image\n```\nEdit your upload actions as so\n```\npublic function actions(){\n    return [\n           'upload'=\u003e[\n               'class'=\u003e'rustamwin\\filekit\\actions\\UploadAction',\n               ...\n               'on afterSave' =\u003e function($event) {\n                    /* @var $file \\League\\Flysystem\\File */\n                    $file = $event-\u003efile;\n                    \n                    // create new Intervention Image\n                    $img = Intervention\\Image\\ImageManager::make($file-\u003eread());\n                    \n                    // insert watermark at bottom-right corner with 10px offset\n                    $img-\u003einsert('public/watermark.png', 'bottom-right', 10, 10);\n                    \n                    // save image\n                    $file-\u003eput($img-\u003eencode());\n               }\n               ...\n           ]\n       ];\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frustamwin%2Fyii2-filekit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frustamwin%2Fyii2-filekit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frustamwin%2Fyii2-filekit/lists"}