[SOLVED] Problem with CUploadedFile::getInstance

Maybe something changed in 1.0.5, I really don't know…

this is the html:



<?php foreach($gallery as $i => $foto): ?>


<div id="foto-<?php echo $i ?>">


	<div class="simple">


	<?php echo CHtml::activeLabelEx($foto,'titolo_it'); ?>


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


	</div>


	<div class="simple">


	<?php echo CHtml::activeLabelEx($foto,'titolo_en'); ?>


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


	</div>


	<div class="simple">


	<?php echo CHtml::activeLabelEx($foto,'titolo_de'); ?>


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


	</div>


	<div class="simple">


	<?php echo CHtml::activeLabelEx($foto,'titolo_es'); ?>


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


	</div>


	<div class="simple">


	<?php echo CHtml::activeLabelEx($foto,'titolo_fr'); ?>


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


	</div>


	<div class="simple">


	<?php echo CHtml::activeLabelEx($foto,'titolo_ru'); ?>


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


	</div>


	


	<div class="simple">


	<?php echo CHtml::activeLabelEx($foto,'url'); ?>


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


	</div>	


	<br />


</div>


<?php endforeach; ?>


this is how I retrive the datas:



$gallery = array ( new StrutturaFoto, );


		





		


		if(isset($_POST['StrutturaFoto']))


		{


			$valid = true;


			


			foreach ( $_POST['StrutturaFoto'] as $i => $foto ) {


                $gallery[$i] = new StrutturaFoto;   


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





                	$gallery[$i]->attributes = $foto;


                	


                    $gallery[$i]->url = CUploadedFile::getInstance($gallery[$i], "url[$i]" );


                    exit ( print_r($_POST['StrutturaFoto']) );


                   //$photosEvent[$i]->photoUrl = CUploadedFile::getIstance($photosEvent[$i], "photoUrl[$i]" );


                	$valid = $valid && $gallery[$i]->validate();  


                }             


            }


                


                


                


$gallery[$i]->url = CUploadedFile::getInstance($gallery[$i], "url[$i]" );

is always empy but when I try to print the $_POST['StrutturaFoto'] I have:

Array ( [0] => Array ( [titolo_it] => immagine di test [titolo_en] => [titolo_de] => [titolo_es] => [titolo_fr] => [titolo_ru] => [url] => Immagine.JPG ) ) 1

Where's the problem?

Ok I find the error…

$fotoIstance = CUploadedFile::getInstance($fotoz, "url" );

$fotoIstance is NULL so ( as the documentation says ):

the instance of the uploaded file. Null is returned if no file is uploaded for the specified model attribute.

I don't understand where I'm wrong…

In the html page I have:

<div class="simple">


	<?php echo CHtml::activeLabelEx($foto,'url'); ?>


	<?php echo CHtml::activeFileField($foto, "url"); ?>


	</div>	

And this is the php part:

	public function actionTest()


	{


		


		// Adding jQuery to the view page.


		Yii::app()->getClientScript()->registerCoreScript('jquery');		


		$fotoz = new StrutturaFoto;		


		if(isset($_POST['StrutturaFoto']))


		{


			$fotoz->attributes = $_POST['StrutturaFoto']; 			


			$fotoIstance = CUploadedFile::getInstance($fotoz, "url" );


			if ( !$fotoIstance )


				echo "buttibao";


			$fotoz->url = 'struttureFoto/'.$fotoIstance->getName();			


			if( $fotoz->validate() ) {


                Yii::import('application.extensions.image.Image');                 


                $fotoz->strutturaId = $_GET['id'];


                   $fotoIstance->saveAs('struttureFoto/'.$fotoIstance->getName());


                   $image = new Image('struttureFoto/'.$fotoIstance->getName());


                   $image->resize(75, 75);


                   $image->save('struttureFoto/small_'.$fotoIstance->getName());                   


                   $fotoz->save(false);


                $this->redirect(array('show','id'=> $_GET['id'] ));


			}


		}		


		$this->render('test', array(


             'foto' => $fotoz,


             'fotoNumber' => isset($_POST['StrutturaFoto']) ? count($_POST['StrutturaFoto'])-1 : 0, //How many PhotoEvent the user added


             'update' => false,


          ));


		


	}

no one have this problem?

try print_r($_FILES) ?

uhm it's empty… why?

Did you select a file to upload at all? Note, you need to do this again if there's validation error.

Yes I select a file to be uploaded.

The exit(print_r($_FILES)) is made before the validate()

Did you set form's enctype attribute to be multipart/form-data?

damn me you’re right. I’m too tired to find that BIG error :(

Thank you very much qjang :)

thank you. I had the same problem