I added file uploading function to my controller, file is stored in the specified directory, but views like update, index, view shows only the name of the file. I need a link or a button to interact with this uploaded file. For example opening file by pressing on this link or button also downloading this file. Could you help me with it?
Model:
public function rules()
{
return [
...
[['attachment'],'file'],
];
}
Controller:
public function actionCreate()
{
$model = new Letter();
if ($model->load(Yii::$app->request->post())) {
$model->attachment = UploadedFile::getInstance($model, 'attachment');
$filename = pathinfo($model->attachment , PATHINFO_FILENAME);
$ext = pathinfo($model->attachment , PATHINFO_EXTENSION);
$newFname = $filename.'.'.$ext;
$path=Yii::getAlias('@webroot').'/uploads/';
if(!empty($newFname)){
$model->attachment->saveAs($path.$newFname);
$model->attachment = $newFname;
if($model->save()){
return $this->redirect(['view', 'id' => $model->id]);
}
}
}
return $this->render('create', [
'model' => $model,
]);
Form View:
<?php $form = ActiveForm::begin(['options' => ['enctype'=>'multipart/form-data']]); ?>
...
<?= $form->field($model, 'attachment')->fileInput() ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
Thanks in advance.