PHP code example of mongosoft / yii2-upload-behavior
1. Go to this page and download the library: Download mongosoft/yii2-upload-behavior library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mongosoft / yii2-upload-behavior example snippets
class Document extends ActiveRecord
{
/**
* @inheritdoc
*/
public function rules()
{
return [
['file', 'file', 'extensions' => 'doc, docx, pdf', 'on' => ['insert', 'update']],
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getCategory()
{
return $this->hasOne(Category::class, [ 'id' => 'id_category' ]);
}
/**
* @inheritdoc
*/
function behaviors()
{
return [
[
'class' => UploadBehavior::class,
'attribute' => 'file',
'scenarios' => ['insert', 'update'],
'path' => '@webroot/upload/docs/{category.id}',
'url' => '@web/upload/docs/{category.id}',
],
];
}
}
class Controller extends Controller
{
public function actionCreate($id)
{
$model = $this->findModel($id);
$model->setScenario('insert'); // Note! Set upload behavior scenario.
...
...
}
}
$form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]);
class User extends ActiveRecord
{
/**
* @inheritdoc
*/
public function rules()
{
return [
['image', 'image', 'extensions' => 'jpg, jpeg, gif, png', 'on' => ['insert', 'update']],
];
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
[
'class' => \mohorev\file\UploadImageBehavior::class,
'attribute' => 'image',
'scenarios' => ['insert', 'update'],
'placeholder' => '@app/modules/user/assets/images/userpic.jpg',
'path' => '@webroot/upload/user/{id}',
'url' => '@web/upload/user/{id}',
'thumbs' => [
'thumb' => ['width' => 400, 'quality' => 90],
'preview' => ['width' => 200, 'height' => 200],
'news_thumb' => ['width' => 200, 'height' => 200, 'bg_color' => '000'],
],
],
];
}
}
$form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]);
class Track extends ActiveRecord
{
public function getArtist()
{
return $this->hasOne(Artist::class, [ 'id' => 'id_artist' ]);
}
public function behaviors()
{
return [
[
'class' => UploadBehavior::class,
'attribute' => 'image',
'scenarios' => ['default'],
'path' => '@webroot/uploads/{artist.slug}',
'url' => '@web/uploads/{artist.slug}',
],
];
}
}
php composer.phar