Reputation: 313
I am using "CMultiFileUpload" to upload multiple files in YII. I am using the following code:
public function actionAddProductImages($id)
{
$model=new ProductImages;
if(isset($_POST['ProductImages']))
{
$model->attributes=$_POST['ProductImages'];
$files = CUploadedFile::getInstancesByName('image');
foreach ($files as $file)
{
//$rnd = rand(0,9999);
$fileName = $file->getName();
$model->image = $fileName;
$model->product_id = $id;
$model->sortorder = $_POST['ProductImages']['sortorder'];
if($model->save())
{
//$files->saveAs(Yii::getPathOfAlias('webroot').'/upload/productImage/'.$fileName); // image will uplode to rootDirectory/banner/
$file->saveAs(Yii::getPathOfAlias('webroot').'/upload/productImage/'.$fileName);
//thumbmail---------------start---
Yii::app()->thumb->setThumbsDirectory('/upload/productImage/original/');
Yii::app()->thumb->load(Yii::getPathOfAlias('webroot').'/upload/productImage/'.$fileName)->resize(538,359)->save($fileName);
Yii::app()->thumb->setThumbsDirectory('/upload/productImage/thumb/');
Yii::app()->thumb->load(Yii::getPathOfAlias('webroot').'/upload/productImage/'.$fileName)->resize('0','110')->save($fileName);
Yii::app()->thumb->setThumbsDirectory('/upload/productImage/thumb_70/');
Yii::app()->thumb->load(Yii::getPathOfAlias('webroot').'/upload/productImage/'.$fileName)->resize('0',70)->save($fileName);
}
}
/*Yii::app()->user->setFlash('productImage','productImage has been added successfully');
$this->redirect(array('view','id'=>$model->image_id));*/
}
$this->render('create',array(
'model'=>$model,
));
}
Actually what happens with this code: it saves only the last file's information in databases and uploads all the selected files in the specified folder. please help me to find where i am being wrong. I am new to YII.
Upvotes: 0
Views: 317
Reputation: 1652
try (new image - new row in db, that why new model):
public function actionAddProductImages($id) { if(isset($_POST['ProductImages'])) { $files = CUploadedFile::getInstancesByName('image'); foreach ($files as $file) { $model=new ProductImages; $model->attributes=$_POST['ProductImages']; //$rnd = rand(0,9999); $fileName = $file->getName(); $model->image = $fileName; $model->product_id = $id; $model->sortorder = $_POST['ProductImages']['sortorder']; if($model->save()) { //$files->saveAs(Yii::getPathOfAlias('webroot').'/upload/productImage/'.$fileName); // image will uplode to rootDirectory/banner/ $file->saveAs(Yii::getPathOfAlias('webroot').'/upload/productImage/'.$fileName); //thumbmail---------------start--- Yii::app()->thumb->setThumbsDirectory('/upload/productImage/original/'); Yii::app()->thumb->load(Yii::getPathOfAlias('webroot').'/upload/productImage/'.$fileName)->resize(538,359)->save($fileName); Yii::app()->thumb->setThumbsDirectory('/upload/productImage/thumb/'); Yii::app()->thumb->load(Yii::getPathOfAlias('webroot').'/upload/productImage/'.$fileName)->resize('0','110')->save($fileName); Yii::app()->thumb->setThumbsDirectory('/upload/productImage/thumb_70/'); Yii::app()->thumb->load(Yii::getPathOfAlias('webroot').'/upload/productImage/'.$fileName)->resize('0',70)->save($fileName); } } /*Yii::app()->user->setFlash('productImage','productImage has been added successfully'); $this->redirect(array('view','id'=>$model->image_id));*/ } $this->render('create',array( 'model'=>$model, )); }
Upvotes: 1