My View
<?php $form = ActiveForm::begin([
'id' => 'Items',
'enableClientValidation' => false,
'errorSummaryCssClass' => 'error-summary alert alert-error',
'options' => ['enctype' => 'multipart/form-data', 'accept' => 'image/*']
]);
?>
<?php echo $form->errorSummary($model); ?>
<?php echo FileInput::widget([
'name' => 'photo_image[]',
'model' => $photos,
'options'=>[
'multiple'=>true
],
'pluginOptions' => [
'initialPreview'=>\yii\helpers\ArrayHelper::getValue($items_photos, 'initialPreview'),
'initialPreviewAsData'=>false,
'initialCaption'=>"The Moon and the Earth",
'initialPreviewConfig' => \yii\helpers\ArrayHelper::getValue($items_photos, 'initialPreviewConfig'),
'deleteUrl' => "file-delete",
'uploadUrl' => "file-upload?id=".$model->id,
'overwriteInitial'=>false,
],
]);
?>
<?php ActiveForm::end(); ?>
My Controller
public function actionFileUpload($id){
$model = new Photo();
if(\Yii::$app->request->isPost) {
$photos = new Photo();
$photos->photo_image = UploadedFile::getInstances($photos, 'photo_image');
dd($_POST, $_FILES, $photos);
}
return false;
}
// dd($_POST, $_FILES, $photos);
My Model
class Photo extends BasePhoto
{
const INSERT = 'insert';
const UPDATE = 'update';
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios[self::INSERT] = ['photo_image'];
$scenarios[self::UPDATE] = ['photo_image'];
return $scenarios;
}
public function rules()
{
return [
[['photo_image'], 'safe', 'on' => ['upload', 'update']],
[['photo_image'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg', 'maxFiles' => 15, 'on' => ['insert', 'update']],
];
}
dump($_POST, $_FILES, $photos) in controller actionUpdate
[
0 => [
'file_id' => '0'
]
1 => [
'photo_image' => [
'name' => [
0 => '02151006.JPG'
]
'type' => [
0 => 'image/jpeg'
]
'tmp_name' => [
0 => '/tmp/phpcXtPBi'
]
'error' => [
0 => 0
]
'size' => [
0 => 6651723
]
]
]
2 => common\models\Photo#1
(
[yii\db\BaseActiveRecord:_attributes] => [
'photo_image' => []
]
[yii\db\BaseActiveRecord:_oldAttributes] => null
[yii\db\BaseActiveRecord:_related] => []
[yii\base\Model:_errors] => null
[yii\base\Model:_validators] => null
[yii\base\Model:_scenario] => 'default'
[yii\base\Component:_events] => [
'beforeInsert' => [
0 => [
0 => [
0 => yii\behaviors\TimestampBehavior#2
(
[createdAtAttribute] => 'created_at'
[updatedAtAttribute] => 'updated_at'
[value] => yii\db\Expression#3
(
[expression] => 'NOW()'
[params] => []
)
[attributes] => [
'beforeInsert' => [
0 => 'created_at'
1 => 'updated_at'
]
'beforeUpdate' => 'updated_at'
]
[owner] => common\models\Photo#1(...)
)
1 => 'evaluateAttributes'
]
1 => null
]
1 => [
0 => [
0 => yiidreamteam\upload\ImageUploadBehavior#4
(
[attribute] => 'photo_image'
[createThumbsOnSave] => true
[createThumbsOnRequest] => false
[thumbs] => [
'thumb' => [
'width' => 550
'height' => 700
]
'large' => [
'width' => 600
'height' => 800
]
]
[thumbPath] => '@webroot/uploads/items/[[attribute_items_id]]/photos/[[attribute_id]].[[extension]]'
[thumbUrl] => '/uploads/items/[[attribute_items_id]]/photos/[[attribute_id]].[[extension]]'
[filePath] => '@frontend/private/items/[[attribute_items_id]]/photos/[[attribute_id]]_original.[[extension]]'
[fileUrl] => '/private/items/[[attribute_items_id]]/photos/[[attribute_id]]_original.[[extension]]'
[parentRelationAttribute] => null
[*:file] => null
[owner] => common\models\Photo#1(...)
)
1 => 'beforeSave'
]
1 => null
]
]
'beforeUpdate' => [
0 => [
0 => [
0 => yii\behaviors\TimestampBehavior#2(...)
1 => 'evaluateAttributes'
]
1 => null
]
1 => [
0 => [
0 => yiidreamteam\upload\ImageUploadBehavior#4(...)
1 => 'beforeSave'
]
1 => null
]
]
'beforeValidate' => [
0 => [
0 => [
0 => yiidreamteam\upload\ImageUploadBehavior#4(...)
1 => 'beforeValidate'
]
1 => null
]
]
'afterInsert' => [
0 => [
0 => [
0 => yiidreamteam\upload\ImageUploadBehavior#4(...)
1 => 'afterSave'
]
1 => null
]
]
'afterUpdate' => [
0 => [
0 => [
0 => yiidreamteam\upload\ImageUploadBehavior#4(...)
1 => 'afterSave'
]
1 => null
]
]
'beforeDelete' => [
0 => [
0 => [
0 => yiidreamteam\upload\ImageUploadBehavior#4(...)
1 => 'beforeDelete'
]
1 => null
]
]
'afterFileSave' => [
0 => [
0 => [
0 => yiidreamteam\upload\ImageUploadBehavior#4(...)
1 => 'afterFileSave'
]
1 => null
]
]
]
[yii\base\Component:_behaviors] => [
0 => yii\behaviors\TimestampBehavior#2(...)
1 => yiidreamteam\upload\ImageUploadBehavior#4(...)
]
)
]
i don’t know where was wrong, please help