dev-master
9999999-dev https://github.com/platx/yii2-uploadUseful upload behavior for Yii Framework 2
BSD-3-Clause
The Requires
extension yii2 upload yii image file upload image upload upload behavior
Wallogit.com
2017 © Pedro Peláez
Useful upload behavior for Yii Framework 2
Behavior for ActiveRecord models for easy file uploading. Following formats of uploads are supported: * POST method upload (http://php.net/manual/en/features.file-upload.post-method.php) * Remote file url * Base64 * Server path, (*1)
Package includes 3 classes:
* FileUploadBehavior - main behavior to handle files
* ImageUploadBehavior - extended class to handle image files, includes functional to
fix image orientation for jpeg images, parameter originalFolder to store files in original
size folder (for ImageAction)
* ImageAction - makes different sizes for images, that stores resized files to folder
near original folder, named "{width}x{height}., (*2)
The preferred way to install this extension is through composer., (*3)
Either run, (*4)
php composer.phar require --prefer-dist platx/yii2-upload "*"
or add, (*5)
"platx/yii2-upload": "*"
to the require section of your composer.json file., (*6)
Attach the behavior in your ActiveRecord model:, (*7)
public function behaviors()
{
return [
'upload' => [
'class' => 'platx\upload\FileUploadBehavior',
// or 'class' => 'platx\upload\ImageUploadBehavior',
'attributes' => ['image'], // Model attributes to handle
'scenarios' => ['default'], // Scenarios to handle
'basePath' => '@app/web/uploads', // Base path on server to store files
'baseUrl' => '/uploads', // Base url to make url
// Additional configuration
]
];
}
Configuration options for platx\upload\FileUploadBehavior:, (*8)
array. Required parameter.array.string. Default value: file_.string. Default value: @app/web/uploads.string. Default value: /uploads.boolean. Default value: false.{id}_{name}.{ext}. Can be:
true - Generate new file namefalse - Keep original file namestring - Template to generate new file name, can be with variables:
{id} - Record id (if is composite key, will be imploded){attribute} - Model attribute name{name} - File name{ext} - File extension\Closure - the callback function to be executed. The anonymous
function should have the following signature:
php
function ($model, $attribute, $file)
where:
- $model - Owner model instance
- $attribute - Current attribute name for file upload
- $file - Current UploadedFile instance to save
For example:
php
function ($model, $attribute, $file) {
return 'file_' . $model->id . '.' .$file->extension;
}
true. Can be:
true - Include model folderfalse - Do not include model folderstring - Static string to be included\Closure - The callback function to be executed. The anonymous
function should have the following signature:
php
function ($model, $attribute, $file)
where:
- $model: Owner model instance
- $attribute: Current attribute name for file upload
- $file: Current UploadedFile instance to upload
For example:
php
function ($model, $attribute, $file) {
return 'my_' . $model::getTableSchema()->fullName;
}
true. Can be:
true - Generate dynamic folder (3 nesting levels), using model primary keyfalse - Do not include dynamic folderstring - Static string\Closure - The callback function to be executed. The anonymous
function should have the following signature:
php
function ($model, $attribute, $file)
where:
- $model: Owner model instance
- $attribute: Current attribute name for file upload
- $file: Current UploadedFile instance to upload
For example:
php
function ($model, $attribute, $file) {
return $model::getTableSchema()->fullName . '_' .uniqid();
}
true. Can be:
true - Add folder with attribute namefalse - Do not include attribute folderstring - Static string\Closure - The callback function to be executed. The anonymous
function should have the following signature:
php
function ($model, $attribute, $file)
where:
- $model: Owner model instance
- $attribute: Current attribute name for file upload
- $file: Current UploadedFile instance to upload
For example:
php
function ($model, $attribute, $file) {
return $model::getTableSchema()->fullName . '_' .uniqid();
}
boolean. Default value: false.boolean. Default value: true.boolean. Default value: true.string. Default value: From message source.string. Default value: From message source.string. Default value: From message source.string. Default value: From message source.string. Default value: original.Then to enable file validation you should add it to rules array of your model, like this:, (*9)
public function rules()
{
return [
['file_image', 'file'],
// or
['file_image', 'image'],
];
}
Attach the image action in your controller class:, (*10)
public function actions()
{
return [
'upload' => [
'image' => [
'class' => \platx\upload\ImageAction::className(),
'basePath' => '@app/web/uploads',
'originalFolder' => 'original',
'sizeList' => ['500x500','200x0','0x300']
],
]
];
}
where:
- sizeList - Allowed size list of generated images, if is empty, any size is allowed to resize.
If you will put 0 to width or height, it will be dynamic to save image ratio.
- basePath - Base server path to image uploads
- originalFolder - Folder name with original image files, (*11)
Add following to your UrlManager component rules:, (*12)
'/uploads/<width:[\d]+>x<height:[\d]+>/<link:[\w\d\/-_\.]+>' => '/your_controller/image'
To upload file, use attribute name {prefix}_{attribute}.
To get file url, use function getFileUrl($attribute, $isAbsolute) for FileUploadBehavior and
getFileUrl($attribute, $isAbsolute, $size) for ImageUploadBehavior with ImageAction configured,
where:
- $attribute - your attribute name
- $isAbsolute - whether to make your file url absolute or not (with http/https and your site domain)
- $size - Size to needed size of image in {width}x{height} format, (*13)
For example, you have attribute named image and behavior property prefix equals file_.
To upload file, you should use attribute file_image.
To get file url, you should use function of your model $model->getFileUrl('image')., (*14)
Example form file:, (*15)
<?php $form = \yii\bootstrap\ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model, 'file_image')->fileInput() ?>
<div class="form-group">
<?= \yii\helpers\Html::submitButton('Upload', ['class' => 'btn btn-primary']) ?>
</div>
<?php $form->end(); ?>
Example view file:, (*16)
<?= \yii\helpers\Html::img($model->getFileUrl('image')); ?>
or using with ImageAction configured:, (*17)
<?= \yii\helpers\Html::img($model->getFileUrl('image', false, '100x100')); ?>
Useful upload behavior for Yii Framework 2
BSD-3-Clause
extension yii2 upload yii image file upload image upload upload behavior