Recommend this page to a friend! |
Download |
Info | Documentation | Files | Install with Composer | Download | Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2024-03-25 (6 months ago) | Not enough user ratings | Total: 15 | All time: 11,366 This week: 73 |
Version | License | PHP version | Categories | |||
yii2-uploader-behavi 1.0.0 | BSD License | 5 | PHP 5, Databases, Data types, Design ... |
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.
The preferred way to install this extension is through composer.
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.
Once the extension is installed, simply use it in your models by :
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.
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:
$form->field($model, 'image')->fileInput();
For a more controlled usage, you can specify which attributes to control using the attributes
option as in :
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:
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:
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.
Files (5) |
File | Role | Description |
---|---|---|
CHANGELOG.md | Data | Auxiliary data |
composer.json | Data | Auxiliary data |
LICENSE.md | Lic. | License text |
README.md | Doc. | Documentation |
UploaderBehavior.php | Class | Class source |
The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
Install with Composer |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.