CostaRico / yii2-images

Yii2-images - behavior-based module for resizing, storing, caching and attaching images.
160 stars 98 forks source link

how can i do multiple upload? #32

Open gorsargsyan opened 9 years ago

gorsargsyan commented 9 years ago

// Controller

public function actionUpdate($id) { $model = $this->findModel($id);

    if ($model->load(Yii::$app->request->post())) {
        $model->image = \yii\web\UploadedFile::getInstance($model, 'image');
        if($model->image){
             foreach ($this->image as $file) {
            $path = Yii::getAlias('@webroot/uploads/').$model->file->baseName.'.'.$model->file->extension;
            $model->file->saveAs($path);
            $model->attachImage($path);
        }

        }
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('update', [
            'model' => $model,
        ]);
    }
}

//view _form

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

nekit44 commented 8 years ago

$model->image = \yii\web\UploadedFile::getInstances($model, 'image');

нужно прописать, и будет работать

A1exeyDunin commented 8 years ago

`public function actionUpdate($id) { $model = $this->findModel($id);

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        $model->image = UploadedFile::getInstances($model, 'image');
        foreach ($model->image as $item) {
            $path = Yii::getAlias('@webroot/upload/files/') . $item->baseName . '.' . $item->extension;
            $item->saveAs($path);
            $model->attachImage($path);
        }
        return $this->redirect($url = Url::previous());
    } else {
        return $this->render('update', [
            'model' => $model,
        ]);
    }
}`