{"id":14966730,"url":"https://github.com/coderius/yii2-upload-file-behavior","last_synced_at":"2026-02-25T14:04:28.991Z","repository":{"id":56955643,"uuid":"288551831","full_name":"coderius/yii2-upload-file-behavior","owner":"coderius","description":"Upload file behavior for Yii2","archived":false,"fork":false,"pushed_at":"2020-08-31T15:07:28.000Z","size":607,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-25T09:14:21.128Z","etag":null,"topics":["behavior","file","image","upload-file","upload-images","upload-pictures","uploader","yii2","yii2-behavior","yii2-behaviors-upload-file","yii2-extension","yii2-modules","yii2-save-file","yii2-upload-file","yii2-upload-image"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/coderius.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-08-18T19:57:15.000Z","updated_at":"2021-05-02T21:29:19.000Z","dependencies_parsed_at":"2022-08-21T08:50:36.058Z","dependency_job_id":null,"html_url":"https://github.com/coderius/yii2-upload-file-behavior","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coderius%2Fyii2-upload-file-behavior","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coderius%2Fyii2-upload-file-behavior/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coderius%2Fyii2-upload-file-behavior/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coderius%2Fyii2-upload-file-behavior/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/coderius","download_url":"https://codeload.github.com/coderius/yii2-upload-file-behavior/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239171868,"owners_count":19594050,"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":["behavior","file","image","upload-file","upload-images","upload-pictures","uploader","yii2","yii2-behavior","yii2-behaviors-upload-file","yii2-extension","yii2-modules","yii2-save-file","yii2-upload-file","yii2-upload-image"],"created_at":"2024-09-24T13:36:51.898Z","updated_at":"2025-10-24T11:32:28.556Z","avatar_url":"https://github.com/coderius.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Yii2 upload file behavior #\n[![Software License](https://img.shields.io/github/license/coderius/yii2-upload-file-behavior)](LICENSE.md)\n[![Latest Version](https://img.shields.io/github/tag/coderius/yii2-upload-file-behavior.svg?style=flat-square\u0026label=release)](https://github.com/coderius/yii2-upload-file-behavior/tags)\n[![Code Coverage](https://scrutinizer-ci.com/g/coderius/yii2-upload-file-behavior/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/coderius/yii2-upload-file-behavior/?branch=master)\n[![Code Intelligence Status](https://scrutinizer-ci.com/g/coderius/yii2-upload-file-behavior/badges/code-intelligence.svg?b=master)](https://scrutinizer-ci.com/code-intelligence)\n[![Code Quality](https://img.shields.io/scrutinizer/quality/g/coderius/yii2-upload-file-behavior.svg?style=flat-square)](https://scrutinizer-ci.com/g/coderius/yii2-upload-file-behavior/?branch=master)\n[![Build Status](https://scrutinizer-ci.com/g/coderius/yii2-upload-file-behavior/badges/build.png?b=master)](https://scrutinizer-ci.com/g/coderius/yii2-upload-file-behavior/build-status/master)\n[![Total Downloads](https://img.shields.io/packagist/dt/coderius/yii2-upload-file-behavior.svg?style=flat-square)](https://packagist.org/packages/coderius/yii2-upload-file-behavior)\n\n\n## About\nYii2 upload file behavior - simple wey to upload images and files to server. \nNo need anymore wrote tonn of code in controller and else testing it by houers. As a result - saving time and labor costs for uploading files to the site.\nOnly needed upload extention from github and past some less code to model class (\\yii\\db\\ActiveRecord) where needed hendler uploading files.\nMore on this below.\n\n## Installation\n\nThe preferred way to install this extension is through [composer](http://getcomposer.org/download/).\n\nFirst download extention. Run the command in the terminal:\n```\ncomposer require \"coderius/yii2-upload-file-behavior\"\n```\n\nor add in composer.json\n```\n\"coderius/yii2-upload-file-behavior\": \"^1.0\"\n```\nand run `composer update`\n\n## Usage\nThis extention created for usage in \\yii\\db\\ActiveRecord model classes.\n\nConfigyration behavior.\n\n* __$nameOfAttributeFile__ = (string) default name 'file'. Virtual attribute for uploading file instance from file systrem.\n* __$nameOfAttributeStorage__ = (string) default name 'face_img'. Attribute for saving path to uploaded file in db.\n* __$newFileName__ = (string) name which is assigned to uploaded file\n* __$directories__ = (array) configs to upload folder and upload hendlers.Сonsists of separate arrays.\n    Each array contains settings for the path to the target folder and a handler for uploading files to this folder like 'path' and 'handler'\n        __-'path'__ - contains path to target folder\n        __-'hendler'__ - Processes the downloaded file and saves to the specified in param 'path' location.\n\nThis extention created for usage in \\yii\\db\\ActiveRecord model classes.\n\n- So, first in model class put namespace to yii2-upload-file-behavior. \n- Create public variable $file for loading file from filesystem.\n- The database must have an attribute to store the file path. In example below it is 'img_src' attribute (marked like save in public function rules())\n- Then past needed configs behaviors() method like in example.\n\n__!Note.__ _Don't forget to include the dependency namespaces._\n\n**Example**\n\n```\n    namespase your/models;\n\n    use coderius\\yii2UploadFileBehavior\\UploadFileBehavior;\n    use yii\\imagine\\Image;\n    use Imagine\\Image\\Point;\n    use Imagine\\Image\\Box;\n\n    class YourModel extends \\yii\\db\\ActiveRecord\n    {\n        public $file;\n\n        //'img_src' - attribute to save path to file in db\n        public function rules()\n        {\n            return [\n                [['img_src'], 'safe'],\n        }\n\n        ...\n\n            public function behaviors()\n            {\n                return [\n                    //Another behaviors\n                    //...\n\n                    'uploadFileBehavior' =\u003e [\n                        'class' =\u003e UploadFileBehavior::className(),\n                        'nameOfAttributeStorage' =\u003e 'img_src',\n                        'directories' =\u003e [\n                            \n                            [\n                                'path' =\u003e function($attributes){\n                                    return \\Yii::getAlias('@portfoleoPhotosPath/' . $attributes['id'] . '/big/');\n                                },\n                                'hendler' =\u003e function($fileTempName, $newFilePath){\n                                    Image::thumbnail($fileTempName, 900, 900*2/3)\n                                    -\u003ecopy()\n                                    -\u003ecrop(new Point(0, 0), new Box(900, 900*2/3))\n                                    -\u003esave($newFilePath, ['quality' =\u003e 80]);\n                                    sleep(1);\n                                }\n                            ],\n                            [\n                                'path' =\u003e function($attributes){\n                                    return \\Yii::getAlias('@portfoleoPhotosPath/' . $attributes['id'] . '/middle/');\n                                },\n                                'hendler' =\u003e function($fileTempName, $newFilePath){\n                                    Image::thumbnail($fileTempName, 400, 400*2/3)\n                                    -\u003esave($newFilePath, ['quality' =\u003e 80]);\n                                    sleep(1);\n                                }\n                            ],\n                            [\n                                'path' =\u003e function($attributes){\n                                    return \\Yii::getAlias('@portfoleoPhotosPath/' . $attributes['id'] . '/small/');\n                                },\n                                'hendler' =\u003e function($fileTempName, $newFilePath){\n                                    Image::thumbnail($fileTempName, 150, 150*2/3)\n                                    -\u003esave($newFilePath, ['quality' =\u003e 80]);\n                                    sleep(1);\n                                }\n                            ],\n                        ]\n                    ],\n\n                ];\n            }\n\n        ...\n    }    \n\n```\n\nShort simple config:\n```\n'uploadFileBehavior' =\u003e [\n    'class' =\u003e UploadFileBehavior::className(),\n    'nameOfAttributeStorage' =\u003e 'img_src',\n    'newFileName' =\u003e 'image-123',\n    'targets' =\u003e [\n        [\n            'path' =\u003e '@uploadsPath',\n            'hendler' =\u003e [\n                'type' =\u003e UploadFileBehavior::TYPE_IMAGE,\n                'config' =\u003e [\n                    'size' =\u003e [\n                        'width' =\u003e 400,\n                        'height'=\u003e 400*2/3\n                    ],\n                    'quality' =\u003e 80\n                ]\n            ]\n        ]\n    ]\n]        \n```\nIn this case allowed params is: 'size' \u0026 'quality'.\n\n\nAdditional actions:\n-------------------------------------\n1. Create aliases to target folders for saving uploaded files.\n2. Create target folders in 'frontend/web' dirrectory like example.\n3. Don't forget create vertual attribute. If it named like '$file', then no need set config to 'nameOfAttributeFile'(default = (string)'file').\n\n## Testing\n\nRun tests in extention folder.\n\n```bash\n$ ./vendor/bin/phpunit\n```\n\nNote! \nFor running all tests needed upload all dependencies by composer. If tested single extention, then run command from root directory where located extention:\n```\ncomposer update\n```\n\nWhen all dependencies downloaded run all tests in terminal from root folder:\n```\n./vendor/bin/phpunit tests\n```\nOr for only unit:\n```\n./vendor/bin/phpunit --testsuite Unit\n```\n\nIf extention tested in app, then set correct path to phpunit and run some commands.\n\n## Credits\n\n- [Sergio Coderius](https://github.com/coderius)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoderius%2Fyii2-upload-file-behavior","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoderius%2Fyii2-upload-file-behavior","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoderius%2Fyii2-upload-file-behavior/lists"}