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])
        //->select([])
        ->all();

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

$images=[];
        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'),
                'dropZoneClickTitle'=>'',
                "uploadAsync" => false,
                "browseOnZoneClick"=>true,
                'uploadExtraData' => [
                    'album_id' => $id,
                ],

                'initialPreview'=>[
                    $images,
                    //ArrayHelper::getValue($file, 'photos'),
                     //foreach ($images as $image) {
                     //   echo $image;
                    // }
                    "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/FullMoon2010.jpg/631px-FullMoon2010.jpg",
                    "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6f/Earth_Eastern_Hemisphere.jpg/600px-Earth_Eastern_Hemisphere.jpg"
                ],
                'initialPreviewAsData'=>true,
                'initialCaption'=> ArrayHelper::getValue($file, 'photos'),
                'initialPreviewConfig' => [
                 //   ['caption' => 'Moon.jpg', 'size' => '873727'],
                 //   ['caption' => 'Earth.jpg', 'size' => '1287883'],
                ],
                'overwriteInitial'=>false,
                'fileActionSettings' => [
                    'showZoom' => true,
                    'showRemove' => true,
                    'showUpload' => false,
                ],
                'maxFileCount' => 20, 'maxFileSize' => 10000, 'msgPlaceholder' => Yii::t('app', 'Select attachments'),
            ],
            'pluginEvents' => [
                'filebatchselected' => 'function() {
                 $(this).fileinput("upload");
                 }',



            ],
        ]);

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