Answer the question
In order to leave comments, you need to log in
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],
}
<?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(); ?>
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
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.
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 questionAsk a Question
731 491 924 answers to any question