Hi,
I am trying to upload multiple images in a form using Yii. But when submitting the form it is showing ‘image cannot be blank’ to all the file inputs. When debugging I can see the file data in the $_FILES array but the post data shows empty. Below are the code snippets
My Controller
for($i=1; $i<=$_POST['totalCount']; $i++)
{
$model=new ActivityBanners;
$model->attributes=$_POST['ActivityBanners'];
$model->iUId=Yii::app()->session['user_id'];
$model->dtCreatedOn=date('Y-m-d H:i:s');
$model->iStatus=1;
$model->vcType=$_POST['ActivityBanners']['vcType'][$i];
$vcTryImgInstance=CUploadedFile::getInstance($model,'vcImage['.$i.']');
$try_file_name=$_POST['ActivityBanners']['vcType'][$i].'_'.rand().'_'.CustomHelper::fileNameGen($vcTryImgInstance);
if($vcTryImgInstance!='')
$model->vcImage=$try_file_name;
if($model->validate())
{
if($model->save(false))
{
$res++;
if($vcTryImgInstance!='')
{
$vcTryImgInstance->saveAs(Yii::app()->params['bannerUploadDir'].$try_file_name);
$file= Yii::app()->params['bannerUploadDir'].$try_file_name;
$img = Yii::app()->simpleImage->load($file);
$img->resizeToWidth(Yii::app()->params['thumbSmallSize']);
$img->save(Yii::app()->params['uploadBannerThumbDir'].$try_file_name);
}
}
}
}
Model Code:
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('iLangId, vcImage', 'required'),
array('iLangId, iStatus, iUId', 'numerical', 'integerOnly'=>true),
array('vcImage', 'length', 'max'=>512),
array('vcType', 'length', 'max'=>100),
array('dtCreatedOn, dtLastModified', 'safe'),
array('vcImage', 'file', 'types'=>'jpg, gif, png,', 'maxSize' => 1024 * 1024 * 10,'tooLarge' => 'The file was larger than 10MB. Please upload a smaller file.', 'on' => 'insert'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('iBannerId, iLangId, vcImage, vcType, iStatus, dtCreatedOn, dtLastModified, iUId', 'safe', 'on'=>'search'),
);
}
View Code
foreach($activityBannerTypes as $values ) {
$i++;
?>
<div class="row">
<?php echo $form->hiddenField($model,'vcType['.$i.']',array('value'=>$values)); ?>
<?php echo$form->labelEx($model, $values.' '.'Image'); ?>
<?php echo $form->fileField($model,'vcImage['.$i.']'); ?>
<?php echo $form->error($model,'vcImage'); ?>
</div>
<?php }
Thanks in Advance!!