Using Cuploadedfile Issues

I have tried to follow all the suggestions I could find on making the CUploadedFile work, but cannot seem to get there. The return from GetInstance is always null. The file name is returned under $_POST under the model from the view to the controller, but it must not be right. I am not clear on what is to be returned under the model, the entire model or just the file name. All I see is the filename, not sure where to go from here.

Form:

<?php

class XUploadForm extends CFormModel

{

    public &#036;photos;


    


    public function rules()


    {


            return array(


                    array('photos', 'file','types'=&gt;'jpg,gif,png'),


            );


    }

}

View:

<?php

 &#036;form=&#036;this-&gt;beginWidget('CActiveForm', array(


    'id'=&gt;'image_upload',


    'enableAjaxValidation'=&gt;false,


    'htmlOptions'=&gt;array('enctype'=&gt;'multipart/form-data',),


    )); 


 


echo &#036;form-&gt;fileField(new XUploadForm(), 'photos');


echo '&lt;/br&gt;';


echo CHtml::button('Upload', array( 'submit'=&gt; array('asset/upload')));

$this->endWidget();

?>

Controller:

public function actionUpload( ) {





    &#036;model = new XUploadForm();


    


    if(isset(&#036;_POST['XUploadForm'])){


         &#036;model-&gt;attributes = &#036;_POST['XUploadForm'];


         &#036;photos = CUploadedFile::getInstance(&#036;model, 'photos');


    }

Your code is fine, but there are 3 unusual things in it.

  1. You don’t pass your model from controller to view (maybe, you’ve just omitted this code). Instead of that, you create new instance of model in your view.

  2. You don’t validate your model, although there are validation rules in it.

  3. You don’t save uploaded file anywhere. How did you check, that upload process fails?

Did you read this wiki article carefully?

Try this code:

Model:




class XUploadForm extends CFormModel {

    public $photos;


    public function rules() {

        return array(

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

        );

    }

}

Controller:




public function actionUpload( ) {

    $model = new XUploadForm();


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

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

        if ($model->validate()) {

            $photos = CUploadedFile::getInstance($model, 'photos');

            $photos->saveAs('path/to/folder/with/write/permission/for/web/server');

        }

    }


    $this->render('_upload_form', array('model'=>$model));

}



View (_upload_form.php’):




<?php

$form=$this->beginWidget('CActiveForm', array(

    'id'=>'image_upload',

    'enableAjaxValidation'=>false,

    'htmlOptions' => array(

        'enctype'=>'multipart/form-data'

    ),

));


    echo $form->fileField($model, 'photos');

    echo '</br>';

    echo CHtml::button('Upload');


$this->endWidget();

?>

Thanks for the reply. Yes, I am calling the initial render from another controller function. Moving some things around per your suggestion, I have gotten it to work, but seem to have caused another side effect. This view is called from another view which is under a form widget so I effectively have a form within a form. I get the image data back, but it clears out all the original form data from the partialRender which creates this upload form.