https://github.com/daxslab/yii2-uploader-behavior
Simplify the process to upload a file at model level
https://github.com/daxslab/yii2-uploader-behavior
Last synced: 7 days ago
JSON representation
Simplify the process to upload a file at model level
- Host: GitHub
- URL: https://github.com/daxslab/yii2-uploader-behavior
- Owner: daxslab
- License: mit
- Created: 2017-04-02T05:23:30.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-07-09T23:28:50.000Z (almost 2 years ago)
- Last Synced: 2025-02-02T16:12:35.903Z (2 months ago)
- Language: PHP
- Size: 10.7 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
- cuban-opensource - yii2-uploader-behavior
README
Yii2 Uploader Behavior for Active Record
========================================[](http://travis-ci.org/daxslab/yii2-uploader-behavior)
[](https://packagist.org/packages/daxslab/yii2-uploader-behavior)
[](https://packagist.org/packages/daxslab/yii2-uploader-behavior)
[](https://packagist.org/packages/daxslab/yii2-uploader-behavior)
[](https://packagist.org/packages/daxslab/yii2-uploader-behavior)Automates file uploading for every attribute tagged with `file` or `image` validation rule. It also configures the way that filename should be renamed and stored in database as attribute value.
Installation
------------The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run
```
php composer.phar require --prefer-dist daxslab/yii2-uploader-behavior "*"
```or add
```
"daxslab/yii2-uploader-behavior": "*"
```to the require section of your `composer.json` file.
Usage
-----Once the extension is installed, simply use it in your models by :
```php
use daxslab/behaviors/UploaderBehavior;public function behaviors() {
return [
UploaderBehavior::className()
];
}
```This is the simplest way of usage. It will look for all attributes with a `file` or `image` validation rule.
```php
public function rules()
{
return [
[['name'], 'required'],
[['name'], 'string', 'max' => 256],
[['image'], 'image', 'skipOnEmpty' => true],
];
}
```And update the form to specify a file input for the image attribute:
```php
$form->field($model, 'image')->fileInput();
```For a more controlled usage, you can specify which attributes to control using the `attributes` option as in :
```php
use daxslab/behaviors/UploaderBehavior;public function behaviors() {
return [
[
'class' => UploaderBehavior::className(),
'attributes' => ['avatar'] // or you can use the string format as in 'attributes' => 'avatar'
]
];
}
```By default the filename of the uploaded file is randomized, but if you want to configure it you can setup the `renamer`
option:```php
use daxslab/behaviors/UploaderBehavior;public function behaviors() {
return [
[
'class' => UploaderBehavior::className(),
'attributes' => ['avatar'] // or you can use the string format as in 'attributes' => 'avatar'
'renamer' => UploaderBehavior::RENAME_MD5 //will encode the filename with md5()
]
];
}
```also you can specify a custom function to rename the filename:
```php
use daxslab/behaviors/UploaderBehavior;public function behaviors() {
return [
[
'class' => UploaderBehavior::className(),
'attributes' => ['avatar'] // or you can use the string format as in 'attributes' => 'avatar'
'renamer' => function($name, $owner){
return strtoupper($name); //will turn the filename into uppercase
}
]
];
}
```Proudly made by [Daxslab](http://daxslab.com).