public function upload() { foreach ($this->images as $file) { // Working with DB $model = new UserImages(); $model->form_id = $this->id; $model->basename = $file->baseName; $model->extension = $file->extension; $model->save(); // Working with files $file->saveAs(Yii::getAlias('@webroot/uploads/user_images/') . md5($model->id) . '.' . $file->extension); } return true; }
<?php use yii\helpers\Html; use yii\widgets\DetailView; /* @var $this yii\web\View */ /* @var $model app\models\Form */ $this->title = $model->name; $this->params['breadcrumbs'][] = ['label' => 'Админпанель']; $this->params['breadcrumbs'][] = ['label' => 'Анкеты', 'url' => ['index']]; $this->params['breadcrumbs'][] = $this->title; $images = null; foreach (\app\models\UserImages::find()->where(['form_id' => $model->id])->all() as $item) { $images .= Html::img(Yii::getAlias('@web/uploads/user_images/' . md5($item->id) . '.' . $item->extension), ['class' => 'img-thumbnail']); } ?> <div class="form-view"> <h1 class="page-header"><?php echo Html::encode($this->title); ?> </h1> <p> <?php echo Html::a('Удалить', ['delete', 'id' => $model->id], ['class' => 'btn btn-danger', 'data' => ['confirm' => 'Are you sure you want to delete this item?', 'method' => 'post']]); ?> </p> <?php echo DetailView::widget(['model' => $model, 'attributes' => ['id', 'name', 'city', 'phone', 'school', 'class', 'text:ntext', ['attribute' => 'images', 'format' => 'html', 'value' => $images]]]); ?>