Answer the question
In order to leave comments, you need to log in
How to organize multiple file uploads in Yii2?
View
<?php $form = ActiveForm::begin([
'options' => ['enctype'=>'multipart/form-data']
]); ?>
<?= $form->field($model, 'image')->widget(\dosamigos\fileinput\BootstrapFileInput::className(), [
'options' => ['accept' => 'image/*', 'multiple' => true],
'clientOptions' => [
'previewFileType' => 'text',
'browseClass' => 'btn btn-success',
'uploadClass' => 'btn btn-info',
'removeClass' => 'btn btn-danger',
'removeIcon' => '<i class="glyphicon glyphicon-trash"></i> '
]
]);?>
<?php echo Html::submitButton('Update') ?>
<?php ActiveForm::end(); ?>
public function actionTest()
{
$dir = Yii::getAlias('@frontend/../web/uploads/test/');
$uploaded = false;
$model = new Test();
echo '<pre>';
print_r($_POST);
echo '</pre>';
if ($model->load($_POST)) {
$file = UploadedFile::getInstance($model, 'image[]');
echo '<pre>';
print_r($file);
echo '</pre>';
die();
/*
$model->image = $file;
if ($model->validate()) {
$uploaded = $file->saveAs( $dir . $file->name );
}*/
}
return $this->render('test',[
'model' => $model,
'uploaded' => $uploaded,
'dir' => $dir,
]);
}
Array
(
[_csrf] => amwuNnIuaXNeKhlDNhYFITgtZ2E5STw4AgofRkN/DgUBDkpcLXw7Ng==
[Test] => Array
(
[image] =>
)
)
Answer the question
In order to leave comments, you need to log in
$file = UploadedFile::getInstances($model, 'image');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question