model.php
Код: Выделить всё
namespace app\models;
use yii\db\ActiveRecord;
class Post extends ActiveRecord
{
public static function tableName()
{
return 'oth1_post';
}
public $upload;
public function rules()
{
return [
[['title'], 'required'],
[['title'], 'string', 'max' => 255],
[['upload'], 'file'],
];
}
}
Код: Выделить всё
public function actionIndex()
{
$model = new Post();
$this->handlePostSave($model);
return $this->render('index', [
'model' => $model,
]);
}
Код: Выделить всё
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use app\models\Post;
$this->title = 'Загрузка файлов';
$model = new Post();
$posts = Post::find()->all();
?>
<div class="site-index card border-secondary">
<div class="card-header"><h3><?= Html::encode($this->title) ?></h3></div>
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
<div class="body-content card-body">
<div class="form-group row">
<label class="col-sm-4 col-form-label">Файл</label>
<label class="btn btn-outline-primary custom-button">
<?= $form->field($model, 'upload', ['template' => "{label}\n{input}"])->label('Выбрать файл')->fileInput(['class' => 'hidden']) ?>
</label>
</div>
<div class="form-group row">
<label class="col-sm-4 col-form-label">Описание</label>
<div class="col-sm-8">
<?= $form->field($model, 'title')->textInput(['class' => 'form-control'])->label(false) ?>
</div>
</div>
<div class="col-sm-offset-4">
<?= Html::submitButton('Загрузить', ['class' => 'btn btn-primary single-button mb60']) ?>
</div>
<table class="table">
<thead>
<tr>
<th class="col-md-4">Наименование</th>
<th class="col-md-6">Описание</th>
<th class="col-md-2">Действие</th>
</tr>
</thead>
<tbody>
<?php foreach ($posts as $post):?>
<tr>
<td class="hidden"><?php $post->id ?></td>
<td><?php $post->file ?></td>
<td><?php $post->title ?></td>
<td><?php Html::a('Скачать', ['download', 'id'=>$post->id]) ?></td>
</tr>
<?php endforeach;?>
</tbody>
</table>
</div>
<?php ActiveForm::end() ?>
</div>