├── .editorconfig ├── .env.dist ├── .gitignore ├── .scrutinizer.yml ├── .travis.yml ├── LICENSE.md ├── README.md ├── api └── README.md ├── composer.json ├── composer.lock ├── docker-compose.yml ├── docker └── mysql │ └── my.cnf ├── guide └── README.md ├── phpcs.xml ├── phpdoc.xml ├── phpunit.xml.dist ├── src ├── FileManager.php ├── actions │ └── UploadAction.php ├── behaviors │ ├── FileBehavior.php │ └── FileBind.php └── messages │ └── ru │ └── filemanager-yii2.php └── tests ├── BaseTest.php ├── MultipleUploadTest.php ├── README.md ├── ResizeTest.php ├── RulesTest.php ├── SingleUploadTest.php ├── bootstrap.php ├── config ├── console.php └── local │ └── .gitignore ├── data ├── behavior.php ├── controllers │ └── Controller.php ├── files.php ├── files │ ├── 100x100.png │ ├── 300x300-without-extension │ ├── 300x300.png │ └── 500x500.png ├── fixtures │ └── file.php ├── models │ ├── File.php │ ├── News.php │ └── User.php └── views │ └── gallery-item.php ├── migrations ├── m141230_075228_create_file.php ├── m141230_075229_create_news.php └── m161026_103037_create_news_files.php └── yii /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 4 6 | 7 | [*.{html,js,css}] 8 | indent_size = 2 9 | -------------------------------------------------------------------------------- /.env.dist: -------------------------------------------------------------------------------- 1 | COMPOSE_PROJECT_NAME=filemanager_yii2 2 | MYSQL_ROOT_PASSWORD= 3 | MYSQL_USER= 4 | MYSQL_DATABASE= 5 | MYSQL_PASSWORD= 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | Thumbs.db 3 | *.log 4 | .env 5 | 6 | vendor/ 7 | tests/tmp/ 8 | tests/data/files/tmp/ 9 | build-api/ 10 | -------------------------------------------------------------------------------- /.scrutinizer.yml: -------------------------------------------------------------------------------- 1 | tools: 2 | php_sim: false 3 | php_cpd: false 4 | php_pdepend: true 5 | php_analyzer: true 6 | external_code_coverage: 7 | timeout: 600 # Timeout in seconds. 8 | filter: 9 | excluded_paths: 10 | - 'tests/*' 11 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.5 5 | - 5.6 6 | - 7.0 7 | 8 | cache: 9 | directories: 10 | - $HOME/.composer/cache 11 | 12 | before_install: 13 | - pip install --user codecov 14 | 15 | install: 16 | - travis_retry composer self-update && composer --version 17 | - export PATH="$HOME/.composer/vendor/bin:$PATH" 18 | - travis_retry composer install --prefer-dist --no-interaction 19 | 20 | before_script: 21 | - mysql -e 'create database filemanager_yii2_tests;' 22 | - php ./tests/yii migrate --migrationPath=./src/migrations --interactive=0 23 | - php ./tests/yii migrate --migrationPath=./tests/migrations --interactive=0 24 | 25 | script: 26 | - vendor/bin/phpunit --coverage-clover=coverage.clover 27 | 28 | after_script: 29 | - wget https://scrutinizer-ci.com/ocular.phar 30 | - php ocular.phar code-coverage:upload --format=php-clover coverage.clover 31 | 32 | after_success: 33 | - codecov 34 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015, Igor Romanov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FileManager for Yii2 2 | 3 | [![Build Status](https://travis-ci.org/rkit/filemanager-yii2.svg?branch=master)](https://travis-ci.org/rkit/filemanager-yii2) 4 | [![Code Coverage](https://scrutinizer-ci.com/g/rkit/filemanager-yii2/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/rkit/filemanager-yii2/?branch=master) 5 | [![codecov.io](http://codecov.io/github/rkit/filemanager-yii2/coverage.svg?branch=master)](http://codecov.io/github/rkit/filemanager-yii2?branch=master) 6 | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/rkit/filemanager-yii2/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/rkit/filemanager-yii2/?branch=master) 7 | 8 | ## Features 9 | 10 | - Use [flysystem](https://flysystem.thephpleague.com/) 11 | - Multiple file uploads 12 | - Presets (resize, crop) for files 13 | - Create thumbnails on the fly or after upload 14 | - The ability to specify extra fields for files 15 | - The ability to use any library for manipulating files (resize, crop, etc) 16 | 17 | ## Installation 18 | 19 | ``` 20 | composer require rkit/filemanager-yii2 21 | ``` 22 | 23 | ## Documentation 24 | 25 | - [Guide](/guide) 26 | - [API Reference](/api) 27 | 28 | ## Development 29 | 30 | ### Tests 31 | 32 | - [See docs](/tests/#tests) 33 | 34 | ### Coding Standard 35 | 36 | - PHP Code Sniffer — [phpcs.xml](./phpcs.xml) 37 | -------------------------------------------------------------------------------- /api/README.md: -------------------------------------------------------------------------------- 1 | # FileManager for Yii2 2 | 3 | ## Table of Contents 4 | 5 | * [FileBehavior](#filebehavior) 6 | * [fileRelation](#filerelation) 7 | * [fileOption](#fileoption) 8 | * [fileStorage](#filestorage) 9 | * [filePath](#filepath) 10 | * [fileUrl](#fileurl) 11 | * [fileExtraFields](#fileextrafields) 12 | * [files](#files) 13 | * [file](#file) 14 | * [fileRules](#filerules) 15 | * [fileState](#filestate) 16 | * [filePresetAfterUpload](#filepresetafterupload) 17 | * [thumbUrl](#thumburl) 18 | * [thumbPath](#thumbpath) 19 | * [createFile](#createfile) 20 | 21 | ## FileBehavior 22 | 23 | 24 | 25 | 26 | 27 | * Full name: \rkit\filemanager\behaviors\FileBehavior 28 | * Parent class: 29 | 30 | 31 | ### fileRelation 32 | 33 | Get relation 34 | 35 | ```php 36 | FileBehavior::fileRelation( string $attribute ): \ActiveQuery 37 | ``` 38 | 39 | 40 | 41 | 42 | **Parameters:** 43 | 44 | | Parameter | Type | Description | 45 | |-----------|------|-------------| 46 | | `$attribute` | **string** | The attribute name | 47 | 48 | 49 | 50 | 51 | --- 52 | 53 | ### fileOption 54 | 55 | Get file option 56 | 57 | ```php 58 | FileBehavior::fileOption( string $attribute, string $option, mixed $defaultValue = null ): mixed 59 | ``` 60 | 61 | 62 | 63 | 64 | **Parameters:** 65 | 66 | | Parameter | Type | Description | 67 | |-----------|------|-------------| 68 | | `$attribute` | **string** | The attribute name | 69 | | `$option` | **string** | Option name | 70 | | `$defaultValue` | **mixed** | Default value | 71 | 72 | 73 | 74 | 75 | --- 76 | 77 | ### fileStorage 78 | 79 | Get file storage 80 | 81 | ```php 82 | FileBehavior::fileStorage( string $attribute ): \Flysystem 83 | ``` 84 | 85 | 86 | 87 | 88 | **Parameters:** 89 | 90 | | Parameter | Type | Description | 91 | |-----------|------|-------------| 92 | | `$attribute` | **string** | The attribute name | 93 | 94 | 95 | 96 | 97 | --- 98 | 99 | ### filePath 100 | 101 | Get file path 102 | 103 | ```php 104 | FileBehavior::filePath( string $attribute, \yii\db\ActiveRecord $file = null ): string 105 | ``` 106 | 107 | 108 | 109 | 110 | **Parameters:** 111 | 112 | | Parameter | Type | Description | 113 | |-----------|------|-------------| 114 | | `$attribute` | **string** | The attribute name | 115 | | `$file` | **\yii\db\ActiveRecord** | Use this file model | 116 | 117 | 118 | **Return Value:** 119 | 120 | The file path 121 | 122 | 123 | 124 | --- 125 | 126 | ### fileUrl 127 | 128 | Get file url 129 | 130 | ```php 131 | FileBehavior::fileUrl( string $attribute, \yii\db\ActiveRecord $file = null ): string 132 | ``` 133 | 134 | 135 | 136 | 137 | **Parameters:** 138 | 139 | | Parameter | Type | Description | 140 | |-----------|------|-------------| 141 | | `$attribute` | **string** | The attribute name | 142 | | `$file` | **\yii\db\ActiveRecord** | Use this file model | 143 | 144 | 145 | **Return Value:** 146 | 147 | The file url 148 | 149 | 150 | 151 | --- 152 | 153 | ### fileExtraFields 154 | 155 | Get extra fields of file 156 | 157 | ```php 158 | FileBehavior::fileExtraFields( string $attribute ): array 159 | ``` 160 | 161 | 162 | 163 | 164 | **Parameters:** 165 | 166 | | Parameter | Type | Description | 167 | |-----------|------|-------------| 168 | | `$attribute` | **string** | The attribute name | 169 | 170 | 171 | 172 | 173 | --- 174 | 175 | ### files 176 | 177 | Get files 178 | 179 | ```php 180 | FileBehavior::files( string $attribute ): array<mixed,\ActiveRecord> 181 | ``` 182 | 183 | 184 | 185 | 186 | **Parameters:** 187 | 188 | | Parameter | Type | Description | 189 | |-----------|------|-------------| 190 | | `$attribute` | **string** | The attribute name | 191 | 192 | 193 | **Return Value:** 194 | 195 | The file models 196 | 197 | 198 | 199 | --- 200 | 201 | ### file 202 | 203 | Get the file 204 | 205 | ```php 206 | FileBehavior::file( string $attribute ): \ActiveRecord 207 | ``` 208 | 209 | 210 | 211 | 212 | **Parameters:** 213 | 214 | | Parameter | Type | Description | 215 | |-----------|------|-------------| 216 | | `$attribute` | **string** | The attribute name | 217 | 218 | 219 | **Return Value:** 220 | 221 | The file model 222 | 223 | 224 | 225 | --- 226 | 227 | ### fileRules 228 | 229 | Get rules 230 | 231 | ```php 232 | FileBehavior::fileRules( string $attribute, boolean $onlyCoreValidators = false ): array 233 | ``` 234 | 235 | 236 | 237 | 238 | **Parameters:** 239 | 240 | | Parameter | Type | Description | 241 | |-----------|------|-------------| 242 | | `$attribute` | **string** | The attribute name | 243 | | `$onlyCoreValidators` | **boolean** | Only core validators | 244 | 245 | 246 | 247 | 248 | --- 249 | 250 | ### fileState 251 | 252 | Get file state 253 | 254 | ```php 255 | FileBehavior::fileState( string $attribute ): array 256 | ``` 257 | 258 | 259 | 260 | 261 | **Parameters:** 262 | 263 | | Parameter | Type | Description | 264 | |-----------|------|-------------| 265 | | `$attribute` | **string** | The attribute name | 266 | 267 | 268 | 269 | 270 | --- 271 | 272 | ### filePresetAfterUpload 273 | 274 | Get the presets of the file for apply after upload 275 | 276 | ```php 277 | FileBehavior::filePresetAfterUpload( string $attribute ): array 278 | ``` 279 | 280 | 281 | 282 | 283 | **Parameters:** 284 | 285 | | Parameter | Type | Description | 286 | |-----------|------|-------------| 287 | | `$attribute` | **string** | The attribute name | 288 | 289 | 290 | 291 | 292 | --- 293 | 294 | ### thumbUrl 295 | 296 | Create a thumb and return url 297 | 298 | ```php 299 | FileBehavior::thumbUrl( string $attribute, string $preset, \yii\db\ActiveRecord $file = null ): string 300 | ``` 301 | 302 | 303 | 304 | 305 | **Parameters:** 306 | 307 | | Parameter | Type | Description | 308 | |-----------|------|-------------| 309 | | `$attribute` | **string** | The attribute name | 310 | | `$preset` | **string** | The preset name | 311 | | `$file` | **\yii\db\ActiveRecord** | Use this file model | 312 | 313 | 314 | **Return Value:** 315 | 316 | The file url 317 | 318 | 319 | 320 | --- 321 | 322 | ### thumbPath 323 | 324 | Create a thumb and return full path 325 | 326 | ```php 327 | FileBehavior::thumbPath( string $attribute, string $preset, \yii\db\ActiveRecord $file = null ): string 328 | ``` 329 | 330 | 331 | 332 | 333 | **Parameters:** 334 | 335 | | Parameter | Type | Description | 336 | |-----------|------|-------------| 337 | | `$attribute` | **string** | The attribute name | 338 | | `$preset` | **string** | The preset name | 339 | | `$file` | **\yii\db\ActiveRecord** | Use this file model | 340 | 341 | 342 | **Return Value:** 343 | 344 | The file path 345 | 346 | 347 | 348 | --- 349 | 350 | ### createFile 351 | 352 | Create a file 353 | 354 | ```php 355 | FileBehavior::createFile( string $attribute, string $path, string $name ): \ActiveRecord 356 | ``` 357 | 358 | 359 | 360 | 361 | **Parameters:** 362 | 363 | | Parameter | Type | Description | 364 | |-----------|------|-------------| 365 | | `$attribute` | **string** | The attribute name | 366 | | `$path` | **string** | The file path | 367 | | `$name` | **string** | The file name | 368 | 369 | 370 | **Return Value:** 371 | 372 | The file model 373 | 374 | 375 | 376 | --- 377 | 378 | 379 | 380 | -------- 381 | > This document was automatically generated from source code comments on 2016-11-03 using [phpDocumentor](http://www.phpdoc.org/) and [cvuorinen/phpdoc-markdown-public](https://github.com/cvuorinen/phpdoc-markdown-public) 382 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rkit/filemanager-yii2", 3 | "description": "FileManager for Yii2", 4 | "keywords": ["yii2", "extension", "file manager", "manager", "upload", "resize", "files"], 5 | "homepage": "https://github.com/rkit/filemanager-yii2", 6 | "type": "yii2-extension", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Igor Romanov", 11 | "email": "rkit.ru@gmail.com" 12 | } 13 | ], 14 | "support": { 15 | "issues": "https://github.com/rkit/filemanager-yii2/issues?state=open", 16 | "source": "https://github.com/rkit/filemanager-yii2" 17 | }, 18 | "require": { 19 | "yiisoft/yii2": "^2.0" 20 | }, 21 | "scripts": { 22 | "phpdoc": "phpdoc", 23 | "test-prepare": [ 24 | "php tests/yii migrate --migrationPath=@tests/migrations/ --interactive=0" 25 | ], 26 | "test": "phpunit --colors=always", 27 | "test-coverage": "phpunit --coverage-html=tests/tmp/coverage.html --colors=always", 28 | "test-coverage-open": "open tests/tmp/coverage.html/index.html" 29 | }, 30 | "autoload": { 31 | "psr-4": { 32 | "rkit\\filemanager\\": "src" 33 | } 34 | }, 35 | "require-dev": { 36 | "intervention/image": "^2.3.2", 37 | "phpunit/phpunit": "^4.8", 38 | "phpunit/dbunit": "^1.4", 39 | "phpdocumentor/phpdocumentor": "^2.8.0", 40 | "cvuorinen/phpdoc-markdown-public": "^0.1.2", 41 | "league/flysystem": "^1.0", 42 | "creocoder/yii2-flysystem": "^0.8.1", 43 | "squizlabs/php_codesniffer": "3.0.0RC1" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | 3 | services: 4 | mysql: 5 | image: mysql:5.7 6 | ports: 7 | - "3306:3306" 8 | volumes: 9 | - ./docker/mysql/my.cnf:/etc/mysql/conf.d/my.cnf 10 | - data-mysql-5.7:/var/lib/mysql 11 | restart: always 12 | env_file: .env 13 | 14 | volumes: 15 | data-mysql-5.7: 16 | driver: local 17 | -------------------------------------------------------------------------------- /docker/mysql/my.cnf: -------------------------------------------------------------------------------- 1 | [mysqld] 2 | 3 | default_time_zone = '+00:00' 4 | sql_mode = STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE 5 | -------------------------------------------------------------------------------- /guide/README.md: -------------------------------------------------------------------------------- 1 | # Guide 2 | 3 | This extension does not contain any widget for ajax upload. 4 | You can to use any widget, for example: [fileapi-widget-yii2](https://github.com/rkit/fileapi-widget-yii2) 5 | But what is important, is that **the input value should contain the id of a file or an array of ids.** 6 | 7 | ## Usage 8 | 9 | For example, we have a model `News`, and we want to add the ability to upload files. 10 | Let's do it. 11 | 12 | 1. **Configuring components** 13 | 14 | ```php 15 | 'fileManager' => [ 16 | 'class' => 'rkit\filemanager\FileManager', 17 | // 'sessionName' => 'filemanager.uploads', 18 | ], 19 | // any flysystem component for storage, for example https://github.com/creocoder/yii2-flysystem 20 | 'localFs' => [ 21 | 'class' => 'creocoder\flysystem\LocalFilesystem', 22 | 'path' => '@webroot/uploads', 23 | ], 24 | ``` 25 | 26 | 2. **Creating migrations** 27 | 28 | Migration for a model `File` (and of course you need to create the model) 29 | 30 | ```php 31 | php yii migrate/create create_file_table --fields="title:string:notNull:defaultValue(''),name:string:notNull:defaultValue(''),date_create:timestamp,date_update:timestamp" 32 | ``` 33 | > You can add any extra fields, such as `user_id`, `ip`, `size`, `extension` 34 | 35 | Migration for a join table 36 | 37 | ```php 38 | php yii migrate/create create_news_files_table --fields="news_id:integer:notNull:defaultValue(0),file_id:integer:notNull:defaultValue(0)" 39 | ``` 40 | > You can add any extra fields, such as `type` to divide files by type or `position` to set sort order 41 | 42 | 3. **Applying Migrations** 43 | 44 | ```php 45 | php yii migrate 46 | ``` 47 | 48 | 4. **Declaring a relation** 49 | 50 | ```php 51 | public function getPreviewFile() 52 | { 53 | return $this 54 | ->hasMany(File::className(), ['id' => 'file_id']) 55 | ->viaTable('news_files', ['news_id' => 'id']); 56 | } 57 | ``` 58 | 59 | 5. **Adding upload action** 60 | 61 | ```php 62 | public function behaviors() 63 | { 64 | return [ 65 | 'verbs' => [ 66 | 'class' => VerbFilter::className(), 67 | 'actions' => [ 68 | 'preview-upload' => ['post'] 69 | ], 70 | ], 71 | ]; 72 | } 73 | 74 | public function actions() 75 | { 76 | return [ 77 | 'preview-upload' => [ 78 | 'class' => 'rkit\filemanager\actions\UploadAction', 79 | 'modelClass' => 'app\models\News', 80 | 'attribute' => 'preview', 81 | 'inputName' => 'file', 82 | ], 83 | ]; 84 | } 85 | ``` 86 | 87 | 6. **Adding behavior** 88 | 89 | ```php 90 | public function behaviors() 91 | { 92 | return [ 93 | [ 94 | 'class' => 'rkit\filemanager\behaviors\FileBehavior', 95 | 'attributes' => [ 96 | 'preview' => [ 97 | // any flysystem component for storage 98 | 'storage' => 'localFs', 99 | // the base URL for files 100 | 'baseUrl' => '@web/uploads', 101 | // file type (default `image`) 102 | 'type' => 'image', 103 | // relation name 104 | 'relation' => 'previewFile', 105 | // save file path in attribute (default `false`) 106 | 'saveFilePathInAttribute' => true, 107 | // a callback for generating file path 108 | // `function(ActiveRecord $file): string` 109 | 'templatePath' => function ($file) { 110 | $date = new \DateTime(is_object($file->date_create) ? null : $file->date_create); 111 | return '/' . $date->format('Ym') . '/' . $file->id . '/' . $file->name; 112 | }, 113 | // a callback for creating `File` model 114 | // `function(string $path, string $name): ActiveRecord` 115 | 'createFile' => function ($path, $name) { 116 | $file = new File(); 117 | $file->title = $name; 118 | $file->generateName(pathinfo($name, PATHINFO_EXTENSION)); 119 | $file->save(); 120 | return $file; 121 | }, 122 | // core validators 123 | 'rules' => [ 124 | 'imageSize' => ['minWidth' => 300, 'minHeight' => 300], 125 | 'mimeTypes' => ['image/png', 'image/jpg', 'image/jpeg'], 126 | 'extensions' => ['jpg', 'jpeg', 'png'], 127 | 'maxSize' => 1024 * 1024 * 1, // 1 MB 128 | 'maxFiles' => 1, 129 | 'tooBig' => Yii::t('app', 'File size must not exceed') . ' 1Mb' 130 | ]), 131 | // the names[] of presets with callbacks 132 | // `function(string $realPath, string $publicPath, string $thumbPath)` 133 | 'preset' => [ 134 | '220x220' => function ($realPath, $publicPath, $thumbPath) { 135 | // you can to use any library for manipulating with files 136 | Image::make($realPath . $publicPath) 137 | ->fit(220, 220) 138 | ->save($realPath . $thumbPath, 100); 139 | }, 140 | ]), 141 | // the names[] of presets or `*` to apply all 142 | 'applyPresetAfterUpload' => ['220x220'] 143 | ], 144 | ] 145 | ] 146 | ]; 147 | } 148 | ``` 149 | 150 | ## Behavior settings 151 | 152 | ```php 153 | // any flysystem component for storage 154 | 'storage' => 'localFs', 155 | // the base URL for files 156 | 'baseUrl' => '@web/uploads', 157 | // file type (default `image`) 158 | 'type' => 'image', 159 | // multiple files (default `false`) 160 | 'multiple' => false, 161 | // path to template for upload response (the default is an array with id and path of file) 162 | 'template' => null, 163 | // a relation name 164 | 'relation' => 'previewFile', 165 | // save file path in attribute (default `false`) 166 | 'saveFilePathInAttribute' => true, 167 | // save file id in attribute (default `false`) 168 | 'saveFileIdInAttribute' => false, 169 | // a callback for generating file path 170 | // `function(ActiveRecord $file): string` 171 | 'templatePath' => function ($file) { 172 | $date = new \DateTime(is_object($file->date_create) ? null : $file->date_create); 173 | return '/' . $date->format('Ym') . '/' . $file->id . '/' . $file->name; 174 | }, 175 | // a callback for creating `File` model 176 | // `function(string $path, string $name): ActiveRecord` 177 | 'createFile' => function ($path, $name) { 178 | $file = new File(); 179 | $file->title = $name; 180 | $file->generateName(pathinfo($name, PATHINFO_EXTENSION)); 181 | $file->save(); 182 | return $file; 183 | }, 184 | // a callback for updating `File` model, triggered every time after saving model 185 | // important: return model without saving 186 | // `function(ActiveRecord $file): ActiveRecord` 187 | 'updateFile' => function ($file) { 188 | // you can modify attributes (without saving) 189 | return $file; 190 | }, 191 | // a callback for filling extra fields, triggered every time after saving model 192 | // `function(ActiveRecord $file, array $fields): array new extra fields` 193 | 'extraFields' => function ($file, $fields) { 194 | return [ 195 | 'type' => 1, if you want to divide files by type 196 | 'position' => $positions[$file->id], // if you want to set sort order 197 | ]; 198 | }, 199 | // core validators 200 | 'rules' => [ 201 | 'imageSize' => ['minWidth' => 300, 'minHeight' => 300], 202 | 'mimeTypes' => ['image/png', 'image/jpg', 'image/jpeg'], 203 | 'extensions' => ['jpg', 'jpeg', 'png'], 204 | 'maxSize' => 1024 * 1024 * 1, // 1 MB 205 | 'maxFiles' => 1, 206 | 'tooBig' => Yii::t('app', 'File size must not exceed') . ' 1Mb' 207 | ]), 208 | // the names[] of presets with callbacks 209 | // `function(string $realPath, string $publicPath, string $thumbPath)` 210 | 'preset' => [ 211 | '220x220' => function ($realPath, $publicPath, $thumbPath) { 212 | // you can to use any library for manipulating with files 213 | Image::make($realPath . $publicPath) 214 | ->fit(220, 220) 215 | ->save($realPath . $thumbPath, 100); 216 | }, 217 | ]), 218 | // the names[] of presets or `*` to apply all 219 | 'applyPresetAfterUpload' => ['220x220'] 220 | ``` 221 | 222 | ### API 223 | 224 | To get file 225 | 226 | ```php 227 | $model->file('preview'); 228 | ``` 229 | 230 | or by relation name 231 | 232 | ```php 233 | $model->previewFile; 234 | ``` 235 | 236 | If multiple files 237 | 238 | ```php 239 | $model->files('gallery'); 240 | ``` 241 | 242 | To get file full path 243 | 244 | ```php 245 | $model->filePath('preview'); 246 | ``` 247 | 248 | To get file url 249 | 250 | ```php 251 | $model->fileUrl('gallery'); 252 | ``` 253 | 254 | To create a file manually 255 | 256 | ```php 257 | $model->createFile('preview', '/path/to/file', 'title'); 258 | ``` 259 | 260 | To create thumbnail and return url 261 | 262 | ```php 263 | $model->thumbUrl('preview', '200x200'); 264 | ``` 265 | 266 | To create thumbnail and return full path 267 | 268 | ```php 269 | $model->thumbPath('preview', '200x200'); 270 | ``` 271 | 272 | To get extra fields 273 | 274 | ```php 275 | $model->fileExtraFields('preview'); 276 | ``` 277 | 278 | > [See more in API](../api) 279 | -------------------------------------------------------------------------------- /phpcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | Based on PSR-2 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | *.md 12 | */views/* 13 | 14 | 15 | 16 | 17 | 18 | 0 19 | 20 | 21 | 22 | 23 | */migrations/* 24 | 25 | 26 | 27 | 28 | */migrations/* 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /phpdoc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | FileManager for Yii2 4 | 5 | build-api 6 | 7 | 8 | api 9 | 10 | 11 |