Save Multiple Image Files Using Kartik FileInput Widget

拜拜、爱过 提交于 2019-12-06 02:42:20

Try this:

Controller: you should save image name in database('$model->urls')

public function actionUpload()
{
        $model = new Upload();

        if ($model->load(Yii::$app->request->post())) {
            $model->file = UploadedFile::getInstances($model, 'file');
            foreach ($model->file as $key => $file) {

                $file->saveAs('/uploads/'. $file->baseName . '.' . $file->extension);//Upload files to server
                $model->urls .= 'uploads/' . $file->baseName . '.' . $file->extension.'**';//Save file names in database- '**' is for separating images
            }
            $model->save();
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('upload', [
                'model' => $model,
            ]);
        }
}

View

<?= $form->field($model, 'file[]')->widget(FileInput::classname(), [
'options' => ['multiple' => 'true'],
]) ?>

Model

class Upload extends Model
{
public $file;
public function rules()
{
    return [
        [['file'], 'file','maxFiles' => 6],
        [['urls'],'string'],

    ];
}

Another view for showing images

<?php
$images=explode('**',trim($model->urls));
foreach($images as $image)
{
     echo Html::img(Url::to('@web/' . $image, true));
}
?>

I have heard the problem of uploading multiple images with form data for quite some time. This was my solution hope it can help anybody

controller

public function actionCreate()
{
    $createRoom = new Room();

    if ($createRoom->load(Yii::$app->request->post())) {
        $createRoom->roomFiles = UploadedFile::getInstances($createRoom, 'roomFiles');

        foreach ($createRoom->roomFiles as $key => $file) {
            $file->saveAs('uploads/'. $file->baseName . '.' . $file->extension);//Upload files to server
            $createRoom->room_images .= 'uploads/' . $file->baseName . '.' . $file->extension.'**';//Save file names in database- '**' is for separating images
        }
        $createRoom->save(false);
        return $this->redirect(['view', 'id' => $createRoom->room_id]);
    } else {
        return $this->render('create', [
            'createRoom' => $createRoom,
        ]);
    }
}

create view

<?= $form->field($createRoom, 'roomFiles[]')->fileInput(['multiple' => true, 'accept' => 'image/*']) ?>

    <div class="form-group">
        <?= Html::submitButton($createRoom->isNewRecord ? 'Post' : 'Update', ['class' => $createRoom->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

Room model

public function rules()
    {
       return [

 [['roomFiles'],  'file', 'skipOnEmpty' => false, 'extensions' =>     'png, jpg','maxFiles' => 4],

        ];
    }
Federico Benedetti

In my case i must set save as path in different way

$uploadDir =  Yii::getAlias('@webroot/') . Yii::getAlias('@web/uploads');
foreach ($model->file as $key => $file) {
    $file->saveAs($uploadDir . '/'. $file->baseName . '.' . $file->extension);//Upload files to server
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!