Tuesday, November 3, 2015

Yii2 multple file upload saving in DB issue

I created a sample multiple file upload functionality to upload gallery images...I followed this method.Every thing is working fine..The image saves in the directory also good.but my problem is when I select multiple images to save in DB,It only saves single image name and I want to save all image name.How Can I rectify this error.
Can any body help me???
MY controller



public function actionCreate()
{
$model = new Gallery();


if ($model->load(Yii::$app->request->post()) && $model->save()) {
$model->gallery_image = UploadedFile::getInstances($model, 'gallery_image');

if ($model->upload()) {

// file is uploaded successfully
}

return $this->redirect(['view', 'id' => $model->gallery_id]);

} else {
return $this->render('create', [
'model' => $model,
]);
}
}


my Model



 public function upload()
{

$gal_id = $this->gallery_parent_id;

if ($this->validate()) {

$destination = $this->upload_dir.'/gallery/'.$gal_id;
if(!is_dir($destination))
mkdir($destination);

foreach ($this->gallery_image as $file) {
$ext = end((explode(".", $file)));
$filename = Yii::$app->security->generateRandomString().".{$ext}";

$file->saveAs('images/apartment/gallery/'.$gal_id.'/'. $filename);
// $file->save();
$this->gallery_image = ''.$filename;
$this->save();


}
return true;


} else {
return false;
}


}

No comments:

Post a Comment