H
H
hollanditkzn2017-03-31 12:40:22
Yii
hollanditkzn, 2017-03-31 12:40:22

How to show previously uploaded files in fileinput()?

How to implement so that in fileinput(), if the file was previously uploaded to the server, Now it constantly shows near the button the file is not selected and if left and saved, it will be deleted if saved
Tried to do fileinput(['value' => $model- >img). But that won't help
In the table model

class Zakaz extends ActiveRecord
{
    public $file;
public function rules()
    {
        return [
[['file'], 'file', 'skipOnEmpty' => true],
            [['img'], 'string', 'max' => 100],
}

In the shape of
<?php $form = ActiveForm::begin([
        'options' => ['enctype' => 'multipart/form-data']
    ]); ?>
<?= $form->field($model, 'file')->fileInput() ?>
<div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Создать' : 'Сохранить', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>
<?php ActiveForm::end(); ?>

And in the controller
public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post())) {
            $model->file = UploadedFile::getInstance($model, 'file');
            if(isset($model->file)){
            $model->file->saveAs('attachment/'.$model->id_zakaz.'.'.$model->file->extension);
            $model->img = $model->id_zakaz.'.'.$model->file->extension;}
            
            $model->save();

            return $this->redirect(['view', 'id' => $model->id_zakaz]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
mitaichik, 2017-03-31
@hollanditkzn

There is no way you can do this in input. Here you need to either display an inscription next to it that the file has already been uploaded, or hide the input if you do not want to give the opportunity to re-upload the file.

G
Genek2708, 2019-11-11
@Genek2708

public function actionUpdate($id)
{
$model = $this->findModel($id);
$image = $model->file;
if ($model->load(Yii::$app->request->post())) {
$model->file = UploadedFile::getInstance($model, 'file');
if(isset($model->file)){
$model->file->saveAs('attachment/'.$model->id_zakaz.'.'.$model->file->extension);
$model->img = $model->id_zakaz.'.'.$model->file->extension;}
$model->save();
if (empty($model->file) && $image) {
$model->file = $image;
$model->save();
return $this->redirect(['view', 'id' => $model->id_zakaz]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question