Multiple File Uploaded But Only No Record Is Saved

Hi there. I’m trying to implement a multiple file upload. I followed some online tutorial and my end result is all files are uploaded to the directory but only no record is being saved.

Here is my Image model validation rules


array('path', 'file', 'types'=>'jpg, gif, png'),

array('path', 'file', 'allowEmpty'=>true, 'maxFiles'=>5),

In my form view I have the following widget in place


<?php echo $form->labelEx($image, 'path'); ?>

	<?php $this->widget('CMultiFileUpload',array(

		'model'=>$image,

		'attribute'=>'path',

		'accept'=>'png|jpg|gif',

		'denied'=>'Only .png ,.jpg and .gif images are allowed.', 

		'max'=>5,

		'remove'=>'[x]',

		'duplicate'=>'You have already selected this file.',

	));?>

	<?php echo $form->error($image, 'path'); ?>

And lastly in my controller




$images=CUploadedFile::getInstances($image,"path");

foreach ($images as $i => $pic) {

	if ($pic->saveAs(Yii::getPathOfAlias('webroot').'/files/'.$pic->name)) {

		$image->post_id=$model->id;

		$image->path=$pic->name;

		$image->validate();

		$image->save();

	}

}



Now, if I set


save()

to


$image->save(false);

, it will only save one record.

I also tried dumping out the data of uploaded files and it shows all files. Am I missing something here?

Thanks.

Anyone? Help?

In case of validation fail validate() and save() will return false and nothing will happen.

See $image->errors for details.

in your loop you must create new instance of $image each time




                $image = new YouModelImageClass();

                $image->post_id=$model->id;

                $image->path=$pic->name;

                $image->validate();

                $image->save();        



Thanks for the tips guys. It was indeed a validation failed that made it failed to save. What I did was removing the validation rule one by one to see which one gave the problem.

The culprit was this validation rule in the Image model.


array('path', 'file', 'types'=>'jpg,gif,png')

Once I commented out this validation rule, I was able to save. I believe this validation rule is only applicable to single file upload and not multiple file upload.

Anyhow, thanks guys.