Fileinput() Defaults To 'no File Chosen'

I’m using

 <?= $form->field( $model, 'imageFile')->fileInput() ?> 

to allow a user to input a file, which is working fine when a file hasn’t previously been chosen.

However if a user is to try to update their file after it has already been chosen, I am trying to get rid of the ‘No file chosen’ that appears to the right of the ‘choose file’ button, and instead have it display the name of the file.

I have already tried to set the value of the ‘imageFile’ like you are able to do with normal textInputs. It seems that this doesn’t work for fileInput() though.

 <?= $form->field( $model, 'imageFile', ['inputOptions' => ['value' => $model->imageFile]])->fileInput() ?> 

Anybody know of a solution?

1 Like

You would need to create a condition and display the image

if ($uploaded_file_exists) {

   echo '<img src="/path/to/uploaded.jpg">';


echo $form->field( $model, 'imageFile')->fileInput();

But if you want this all better and automated and with more other features for preview, you can try the FileInput widget. You can use it simple like this in this widget:

use kartik\widgets\FileInput;

echo $form->field($model, 'imageFile')->widget(FileInput::classname(), [

    'pluginOptions' => [

        'initialPreview'=> ($uploaded_file_exists) ?

            Html::img("/images/moon.jpg", ['class'=>'file-preview-image']) :




1 Like

Perfect! As always, your widget is great. Thanks for the help!

1 Like

thanks bro1