File Upload Behavior
php composer.phar require --prefer-dist yiisoft/yii2-imagine
Install migrations
yii migrate --migrationPath=@vendor/suver/yii2-behavior-upload/migrations
Create @storage alias for you structur
Configure you nginx server like this
server {
charset utf-8;
client_max_body_size 128M;
listen 80; ## listen for ipv4
server_name storage.example.com;
root /var/www/com/example/storage/;
index index.php;
location / {
}
}
You must add behavior like this
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'value' => new Expression('NOW()'),
],
[
'class' => \suver\behavior\upload\UploadBehavior::className(),
'attribute' => 'photo',
'thumbnail' => [
'admin_preview' => ['size' => '200x200', 'prefix' => 'v1'],
'admin_preview_without_animate' => ['size' => '100x100', 'prefix' => 'v2', 'option' => ['jpeg_quality' => 10], 'animate' => false],
'medium2' => ['size' => 'x100'],
'medium3' => ['size' => '100'],
],
],
];
}
You must add rule for file like this
/**
* @inheritdoc
*/
public function rules()
{
return [
['photo', 'file', 'extensions' => ['jpg','png','gif'], 'maxSize' => 10*1024*1024, 'maxFiles' => 1]
];
}
Upgrade your upload form like this
php
<!-- Get Uploaded image -->
<img src="<?php echo $model->linkedFile('photo')->thumbnail('admin_preview')->byDefault('/images/gogol.jpg') ?>">
<!-- Render upload input -->
<?= $form->field($model, 'photo')->fileInput() ?>
Its all of you need create for uploaded files for you model )
You don`t create data base or uploaded logic. All you need it add behafior and smoke sigars ))
REMEMBER: Your attribute for file from UploadBehavior options mast not exists in your model class
animate == false
you gif animation is offUploadedFile::getInstancesByName
UploadBehavior::$types
WARNING: If you wont to lot upload you must add multiUpload=true
to your behavior options
Like Example
<img src="<?php echo $model->linkedFile('photo')->thumbnail('admin_preview')->byDefault('/images/gogol.jpg') ?>">
Other method
$model->addFile(/var/www/com/example/storage/er/rt/errtsjdhfjsdhsdfsdfsd/errtsjdhfjsdhsdfsdfsd.jpg);
// Add file from path
$model->addHttpFile(http://storage.example.com/er/rt/errtsjdhfjsdhsdfsdfsd/errtsjdhfjsdhsdfsdfsd.jpg);
// Add file from http
$model->httpFileExists(http://storage.example.com/er/rt/errtsjdhfjsdhsdfsdfsd/errtsjdhfjsdhsdfsdfsd.jpg);
// return TRUE if remote file is exists else FALSE
var_dump($model->linkedFiles('photo'));
//=> [
// ImageFile object,
// File object,
// ImageFile object,
// ]
// return list of uploaded file object
echo $model->linkedFile('photo')
// if exists you see http://storage.example.com/er/rt/errtsjdhfjsdhsdfsdfsd/errtsjdhfjsdhsdfsdfsd.jpg
// if NOT exists you see ''
$model->linkedFile('photo')->thumbnail('admin_preview')
// if exists you see http://storage.example.com/er/rt/errtsjdhfjsdhsdfsdfsd/v1_errtsjdhfjsdhsdfsdfsd_200x200.jpg
// if NOT exists you see ''
$model->linkedFile('photo')->thumbnail('admin_preview')->byDefault('/images/gogol.jpg')
// if exists you see http://storage.example.com/er/rt/errtsjdhfjsdhsdfsdfsd/v1_errtsjdhfjsdhsdfsdfsd_200x200.jpg
// if NOT exists you see /images/gogol.jpg
$model->linkedFile('photo')->getName();
// => errtsjdhfjsdhsdfsdfsd
$model->linkedFile('photo')->getExtension();
// => jpg
$model->linkedFile('photo')->delete();
// => delete you file
$model->linkedFile('photo')->getPath();
// => /er/rt/errtsjdhfjsdhsdfsdfsd/errtsjdhfjsdhsdfsdfsd.jpg
$model->linkedFile('photo')->getFullPath();
// => /var/www/com/example/storage/er/rt/errtsjdhfjsdhsdfsdfsd/errtsjdhfjsdhsdfsdfsd.jpg
$model->linkedFile('photo')->getDomainPath();
// => http://storage.example.com/er/rt/errtsjdhfjsdhsdfsdfsd/errtsjdhfjsdhsdfsdfsd.jpg
$model->linkedFile('photo')->hasFile();
// => TRUE if file exists else FALSE
$model->linkedFile('photo')->getType();
// => UploadBehavior::TYPE_IMAGE == 1
$model->linkedFile('photo')->getMimeType();
// => image/jpeg
$model->linkedFile('photo')->getSize();
// => 1545454 - image size
$model->linkedFile('photo')->getOriginalName();
// => avatar.jpg
$model->linkedFile('photo')->getDirectory()
// => /er/rt/errtsjdhfjsdhsdfsdfsd
$model->linkedFile('photo')->getParams();
// => [width => 1000, height => 1500]
$model->linkedFile('photo')->getHash();
// => sdfsdfsfsdfsdfhsdjfh**sdfsdf - MD5 hash for file content
Comments