Update Images

i try with this code $model->attributes=$_POST[‘Student’];

StudentController.php

<?php

class StudentController extends Controller

{

/**


 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning


 * using two-column layout. See 'protected/views/layouts/column2.php'.


 */


public &#036;layout='//layouts/column2';





/**


 * @return array action filters


 */


public function filters()


{


	return array(


		'accessControl', // perform access control for CRUD operations


		'postOnly + delete', // we only allow deletion via POST request


	);


}





/**


 * Specifies the access control rules.


 * This method is used by the 'accessControl' filter.


 * @return array access control rules


 */


public function accessRules()


{


	return array(


		array('allow',  // allow all users to perform 'index' and 'view' actions


			'actions'=&gt;array('index','view'),


			'users'=&gt;array('*'),


		),


		array('allow', // allow authenticated user to perform 'create' and 'update' actions


			'actions'=&gt;array('create','update'),


			'users'=&gt;array('@'),


		),


		


		array('deny',  // deny all users


			'users'=&gt;array('*'),


		),


	);


}





/**


 * Displays a particular model.


 * @param integer &#036;id the ID of the model to be displayed


 */


public function actionView(&#036;id)


{


	&#036;this-&gt;render('view',array(


		'model'=&gt;&#036;this-&gt;loadModel(&#036;id),


	));


}





/**


 * Creates a new model.


 * If creation is successful, the browser will be redirected to the 'view' page.


 */


public function actionCreate()


{





	&#036;model=new Student;


            





	// Uncomment the following line if AJAX validation is needed


	// &#036;this-&gt;performAjaxValidation(&#036;model);





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


	{


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

//=======================================================Image Upload======================================================================//

                    &#036;model-&gt;student_image=CUploadedFile::getInstance(&#036;model,'student_image');


		if(&#036;model-&gt;save()){


                            


                            &#036;model-&gt;student_image-&gt;saveAs(Yii::app()-&gt;basePath.'/upload/'.&#036;model-&gt;student_image);


                            


			&#036;this-&gt;redirect(array('view','id'=&gt;&#036;model-&gt;student_id));


            }}





	&#036;this-&gt;render('create',array(


		'model'=&gt;&#036;model,


                    


	));


}





/**


 * Updates a particular model.


 * If update is successful, the browser will be redirected to the 'view' page.


 * @param integer &#036;id the ID of the model to be updated


 */


public function actionUpdate(&#036;id)


{


	&#036;model=&#036;this-&gt;loadModel(&#036;id);





	// Uncomment the following line if AJAX validation is needed


	// &#036;this-&gt;performAjaxValidation(&#036;model);





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


	{


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

//===================================================If Already Exist File======================================================================//

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


                    if ( (is_object(&#036;student_image_file) &amp;&amp; get_class(&#036;student_image_file)==='CUploadedFile'))


                    &#036;model-&gt;student_image = &#036;student_image_file;

//===============================================================================================================================================//

		if(&#036;model-&gt;save()){





                        if (is_object(&#036;student_image_file))


                            


                            &#036;model-&gt;student_image-&gt;saveAs(Yii::app()-&gt;basePath.'/upload/'.&#036;model-&gt;student_image);


                          


                         &#036;this-&gt;redirect(array('view','id'=&gt;&#036;model-&gt;student_id));


	}


            }





	&#036;this-&gt;render('update',array(


		'model'=&gt;&#036;model,


	));


}





/**


 * Deletes a particular model.


 * If deletion is successful, the browser will be redirected to the 'admin' page.


 * @param integer &#036;id the ID of the model to be deleted


 */


public function actionDelete(&#036;id)


{


	&#036;this-&gt;loadModel(&#036;id)-&gt;delete();





	// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser


	if(&#33;isset(&#036;_GET['ajax']))


		&#036;this-&gt;redirect(isset(&#036;_POST['returnUrl']) ? &#036;_POST['returnUrl'] : array('admin'));


}





/**


 * Lists all models.


 */

// public function actionIndex()

// {

// $dataProvider=new CActiveDataProvider(‘Student’);

// $this->render(‘index’,array(

// ‘dataProvider’=>$dataProvider,

// ));

// }

/**


 * Manages all models.


 */


public function actionIndex()


{


	&#036;model=new Student('search');


	&#036;model-&gt;unsetAttributes();  // clear any default values


	if(isset(&#036;_GET['Student']))


		&#036;model-&gt;attributes=&#036;_GET['Student'];





	&#036;this-&gt;render('index',array(


		'model'=&gt;&#036;model,


	));


}





/**


 * Returns the data model based on the primary key given in the GET variable.


 * If the data model is not found, an HTTP exception will be raised.


 * @param integer &#036;id the ID of the model to be loaded


 * @return Student the loaded model


 * @throws CHttpException


 */


public function loadModel(&#036;id)


{


	&#036;model=Student::model()-&gt;findByPk(&#036;id);


	if(&#036;model===null)


		throw new CHttpException(404,'The requested page does not exist.');


	return &#036;model;


}





/**


 * Performs the AJAX validation.


 * @param Student &#036;model the model to be validated


 */


protected function performAjaxValidation(&#036;model)


{


	if(isset(&#036;_POST['ajax']) &amp;&amp; &#036;_POST['ajax']==='student-form')


	{


		echo CActiveForm::validate(&#036;model);


		Yii::app()-&gt;end();


	}


}


    public function actionDynamicstates()


   {


        &#036;sql = &quot;SELECT state_name FROM state &quot;.


               &quot;WHERE country_id = :country_id&quot;;


        &#036;command = Yii::app()-&gt;createCommand(&#036;sql);


        &#036;command-&gt;bindValue(':country_id', &#036;_POST['country_id'], PDO::PARAM_INT);


        &#036;data = &#036;command-&gt;query();





        &#036;data = CHtml::listData(&#036;data,'state_id','state_name');


        foreach(&#036;data as &#036;value=&gt;&#036;name)


        {


          echo CHtml::tag('option',


          array('value'=&gt;&#036;value),CHtml::encode(&#036;name),true);


        }


    }

}

student/_form.php

<?php

/* @var $this StudentController */

/* @var $model Student */

/* @var $form CActiveForm */

?>

<div class="form">

<?php $form=$this->beginWidget(‘CActiveForm’, array(

'id'=&gt;'student-form',


'enableAjaxValidation'=&gt;false,


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

)); ?>

&lt;p class=&quot;note&quot;&gt;Fields with &lt;span class=&quot;required&quot;&gt;*&lt;/span&gt; are required.&lt;/p&gt;





&lt;?php echo &#036;form-&gt;errorSummary(&#036;model); ?&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'username'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'username',array('size'=&gt;60,'maxlength'=&gt;255)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'username'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'password'); ?&gt;


	&lt;?php echo &#036;form-&gt;passwordField(&#036;model,'password',array('size'=&gt;15,'maxlength'=&gt;15)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'password'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'student_fname'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'student_fname',array('size'=&gt;60,'maxlength'=&gt;255)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'student_fname'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'student_lname'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'student_lname',array('size'=&gt;60,'maxlength'=&gt;255)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'student_lname'); ?&gt;


&lt;/div&gt;


    


    &lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'student_dob'); ?&gt;


            &lt;?php &#036;this-&gt;widget('zii.widgets.jui.CJuiDatePicker',


                            array(


                            'name' =&gt; 'student_dob',


                            'attribute' =&gt; 'student_dob',


                            'model'=&gt;&#036;model,


                            'options'=&gt; array(


                            'dateFormat' =&gt;'yy-mm-dd',


                            'altFormat' =&gt;'yy-mm-dd',


                            'changeMonth' =&gt; true,


                            'changeYear' =&gt; true,


                            'appendText' =&gt; 'yyyy-mm-dd',


                            ),


                            ));


                            ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'student_dob'); ?&gt;


&lt;/div&gt;


    &lt;div class=&quot;row&quot;&gt;


    &lt;?php echo &#036;form-&gt;labelEx(&#036;model,'country'); ?&gt;


    &lt;?php


          echo &#036;form-&gt;dropDownList(&#036;model,'country_id',  Country::model()-&gt;getCountryOptions());

// array(

// ‘ajax’ => array(

// ‘type’ => ‘POST’,

// ‘url’ => CController::createUrl(‘mymodelname/dynamicStates’),

// ‘update’ => ‘#MyModelName_’.‘state_id’

// )

// )

// );

    ?&gt;


    &lt;?php echo &#036;form-&gt;error(&#036;model,'CountryID'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


    &lt;?php echo &#036;form-&gt;labelEx(&#036;model,'state'); ?&gt;


    &lt;?php echo &#036;form-&gt;dropDownList(&#036;model,'state_id',  State::model()-&gt;getStateOptions());?&gt;


    &lt;?php echo &#036;form-&gt;error(&#036;model,'state_id'); ?&gt;


&lt;/div&gt;


    


    &lt;div class=&quot;row&quot;&gt;


        &lt;?php echo &#036;form-&gt;labelEx(&#036;model,'city_id'); ?&gt;


        &lt;?php echo &#036;form-&gt;dropDownList(&#036;model,'city_id', City::model()-&gt;getCityOptions()); ?&gt;


        &lt;?php echo &#036;form-&gt;error(&#036;model,'city_id'); ?&gt;


    &lt;/div&gt;











&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'gender'); ?&gt;


           &lt;?php echo &#036;form-&gt;dropdownList(&#036;model,'gender',array('1'=&gt;'M','2'=&gt;'F')); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'gender'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'email_address'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'email_address',array('size'=&gt;60,'maxlength'=&gt;255)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'email_address'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'contact_number'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'contact_number',array('size'=&gt;20,'maxlength'=&gt;20)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'contact_number'); ?&gt;


&lt;/div&gt;


    


    &lt;div class=&quot;row&quot;&gt;


               &lt;?php   echo &#036;form-&gt;labelEx(&#036;model, 'student_image');   ?&gt;


               &lt;?php   echo &#036;form-&gt;fileField(&#036;model, 'student_image'); ?&gt;


               &lt;?php   echo &#036;form-&gt;error(&#036;model, 'student_image');     ?&gt;





    &lt;/div&gt;


    &lt;?php if(&#036;model-&gt;isNewRecord&#33;='1'){ ?&gt;


    &lt;div class=&quot;row&quot;&gt;


        


         


      


    &lt;/div&gt;


    &lt;?}?&gt;


&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'package_id'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'package_id'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'package_id'); ?&gt;


&lt;/div&gt;


    


    &lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'package_purchase_date'); ?&gt;


            &lt;?php &#036;this-&gt;widget('zii.widgets.jui.CJuiDatePicker',


                            array(


                            'name' =&gt; 'package_purchase_date',


                            'attribute' =&gt; 'package_purchase_date',


                            'model'=&gt;&#036;model,


                            'options'=&gt; array(


                            'dateFormat' =&gt;'yy-mm-dd',


                            'altFormat' =&gt;'yy-mm-dd',


                            'changeMonth' =&gt; true,


                            'changeYear' =&gt; true,


                            'appendText' =&gt; 'yyyy-mm-dd',


                            ),


                            ));


                            ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'package_purchase_date'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'student_status'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'student_status'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'student_status'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row buttons&quot;&gt;


	&lt;?php echo CHtml::submitButton(&#036;model-&gt;isNewRecord ? 'Create' : 'Save'); ?&gt;


&lt;/div&gt;

<?php $this->endWidget(); ?>

</div><!-- form -->

I solve problem but i can’t see uploaded image problem in baseUrl and basePath

any other suggestion for image upload time of updation of form

Please post the error you get first and please notice there’s a useful code formatting button (<>) at this editor :)

Use this in your create controller :-


$userInfoModel = new UserProfile; 

 

 $rnd = rand(0,9999);  // generate random number between 0-9999

 $uploadedFile=CUploadedFile::getInstance($userInfoModel,'user_img');

 $fileName = "{$rnd}-{$uploadedFile}";


 //save the path in Database


 $userInfoModel->user_img = $fileName;

 $userInfoModel->save();


 //save image in folder


 $root = Yii::app()->basePath.'/../userImage/';

 if(is_dir($root))

 {	

       mkdir($root.$model->id);

 } 

 $uploadedFile->saveAs(Yii::app()->basePath.'/../userImage/'.$model->id.'/'.$fileName); 




;)