Yii Framework Forum: [SOLVED] Problem with CUploadedFile::getInstance - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

[SOLVED] Problem with CUploadedFile::getInstance Rate Topic: -----

#1 User is offline   StErMi 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 259
  • Joined: 30-March 09

Posted 12 May 2009 - 05:18 PM

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?
0

#2 User is offline   StErMi 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 259
  • Joined: 30-March 09

Posted 13 May 2009 - 04:23 AM

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,
          ));

}

0

#3 User is offline   StErMi 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 259
  • Joined: 30-March 09

Posted 13 May 2009 - 08:05 AM

no one have this problem?
0

#4 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,897
  • Joined: 04-October 08
  • Location:DC, USA

Posted 13 May 2009 - 09:49 AM

try print_r($_FILES) ?
0

#5 User is offline   StErMi 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 259
  • Joined: 30-March 09

Posted 13 May 2009 - 10:24 AM

uhm it's empty... why?
0

#6 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,897
  • Joined: 04-October 08
  • Location:DC, USA

Posted 13 May 2009 - 11:33 AM

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

#7 User is offline   StErMi 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 259
  • Joined: 30-March 09

Posted 13 May 2009 - 01:22 PM

Yes I select a file to be uploaded.

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

#8 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,897
  • Joined: 04-October 08
  • Location:DC, USA

Posted 13 May 2009 - 01:30 PM

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

#9 User is offline   StErMi 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 259
  • Joined: 30-March 09

Posted 13 May 2009 - 02:14 PM

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

Thank you very much qjang :)
0

#10 User is offline   Duc Tran 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 14-July 11

Posted 17 August 2011 - 11:26 PM

View Postqiang, on 13 May 2009 - 01:30 PM, said:

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

thank you. I had the same problem
1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users