Kartik FileInput initalpreview

Good Afternoon All,

using the use kartik\widgets\FileInput;
And i have the upload working using AJAX, what im struggling with is setting the initialPreview value so it shows all the current images.
im my controller I am getting the name of the images:

$albumimages = PhotoPhotos::find()
        -> where(['gallery_id'=>$id])

and then loop through to add the full path to an array

        foreach ($albumimages as $image) {
            $images[] = Yii::$app->urlManagerFrontend->createUrl($path . $image->image_thumb_filename);

but if I add $images into the initialPreview it wont load as it doesn’t separate the URLS but i cant seem to put a foreach into the initialPreview section, however it does work if just a single image returned in DB responce

echo FileInput::widget([
            'name' => 'files[]',
            'options' => ['multiple' => true, 'id' => 'unique-id-1'],
            'pluginOptions' => ['allowedFileExtensions' => ['jpg', 'gif', 'png'],
                'previewFileType' => 'any', 'showUpload' => false, 'showRemove' => false, 'initialPreviewAsData' => true,
                "uploadUrl" => Url::to(['/site/uploadimageajax']),
                'msgUploadBegin' => Yii::t('app', 'Please wait, system is uploading the files'),
                'msgUploadThreshold' => Yii::t('app', 'Please wait, system is uploading the files'),
                'msgUploadEnd' => Yii::t('app', 'Done'),
                "uploadAsync" => false,
                'uploadExtraData' => [
                    'album_id' => $id,

                    //ArrayHelper::getValue($file, 'photos'),
                     //foreach ($images as $image) {
                     //   echo $image;
                    // }
                'initialCaption'=> ArrayHelper::getValue($file, 'photos'),
                'initialPreviewConfig' => [
                 //   ['caption' => 'Moon.jpg', 'size' => '873727'],
                 //   ['caption' => 'Earth.jpg', 'size' => '1287883'],
                'fileActionSettings' => [
                    'showZoom' => true,
                    'showRemove' => true,
                    'showUpload' => false,
                'maxFileCount' => 20, 'maxFileSize' => 10000, 'msgPlaceholder' => Yii::t('app', 'Select attachments'),
            'pluginEvents' => [
                'filebatchselected' => 'function() {


What do you get? Is there any error in the console?

Thank you, i worked it out in the end, It was duplicating the array as the square brackets on the initial preview were causing an issue, so when removed and just using the $images it worked perfectly!

1 Like