Multiple file uploads and CUploadedFile / File Validator

First I want to say that I've been using Yii to port my site to PHP from Ruby on Rails and have thus far been very pleased with it.

However, I've come across something that I think may be a bug, though perhaps I'm not setting things up correctly.

I've got file uploading/validating working perfectly with a single file, but with multiple files, both CUploadedFile and the File Validator fail. CUploadedFile doesn't return an object and File Validator fails to see the specific field.

Is it a limitation? Or do I need to do something differently?

Any help would be appreciated.

Thanks!

The following code snippets are condensed by only including pertinent code/markup.

I've got a controller setup like so:



public function actionCreate() {





	...


	


	$images = array();


	$i = 0;


	while($i < 5) {


		$images[] = new ReleaseImage;


		$i++;


	}


	


	...


	


	if(isset($_POST['Release'])) {


		$release->attributes=$_POST['Release'];


		


		if(isset($_POST['submit']) && $release->validate()) {


		


		...


			


			//validate images


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


				if(isset($_POST['ReleaseImage'][$i])) {


					$image->attributes = $_POST['ReleaseImage'][$i];


					$image->image = CUploadedFile::getInstance($image, "imageFile[$i]");


				}


				if(($image->sort !== '') || ($image->caption !== '')) {


					$valid = $valid && $image->validate();


				}


			}


		


		... 


		


		}			


	}





	...		


}


I've got a form with the following:



<?php echo CHtml::form('', 'post', array('enctype' => 'multipart/form-data')); ?>





...





<?php foreach($images as $i => $image): ?>


	<p>


		<?php echo CHtml::activeLabel($image,"sort[$i]"); ?>


		<?php echo CHtml::activeTextField($image,"sort[$i]", array('class' => 'narrow')); ?>


		<?php echo CHtml::activeLabel($image,"type[$i]", array('class' => 'narrow')); ?>


		<?php echo CHtml::activeDropDownList($image, "type[$i]", ReleaseImage::types()); ?>


		<?php echo CHtml::activeLabel($image,"caption[$i]"); ?>


		<?php echo CHtml::activeTextField($image,"caption[$i]"); ?>


	</p>





	<p>


		<?php echo CHtml::activeLabel($image,"imageFile[$i]"); ?>


		<?php echo CHtml::activeFileField($image,"imageFile[$i]"); ?>		


	</p>


	<hr />


<?php endforeach; ?>


In my model I've got the following rule:



public function rules() {


	return array(


		...


		array('imageFile', 'file'),


	);


}


Could you please create a ticket about this? The current implementation of CUploadedFile does not allow this tabular file uploading.

Done. My first submitted ticket. I hope that it's adequate.

Thanks

http://code.google.c…s/detail?id=122

Thanks. I have fixed issue.

You also need some changes to your code.

First, in your model class, you need to declare an attribute that will be used to store the CUploadedFile object. If you already have an attribute that will store the file name, you can simply use that attribute. The validation rule should be about this attribute.

Second, in your action code, please do the following to retrieve files:



foreach($models as $i=>$model)


{


    $model->attributes=$_POST['ModelClass'];


    $model->file=CUploadedFile::getInstance($model,"file[$i]");


    $model->save(); // or call $model->validate() explicitly


}


Your view code doesn't need any change. Let me know if you encounter any problem. Please also double check that your earlier code with single file uploading still works as expected. Thanks for helping!

Thanks for the quick fix! I'll let you know if I come across an issues.

The fix worked perfectly. Thanks again for your quick fix!