File upload

Hi All,

 I am new user of yii,


  I want to upload a file in to my server and files address have to save in a database.


please any one can help me how can i do step by step.

Hi and welcome to the forum…

Since you are new… first thing (if you did not already done it) is to read the Definitive guide to Yii so that you get an overview of how Yii works…

For the upload check this wiki article - http://www.yiiframew…e-using-a-model

And note that you posted in the Yii 1.0 forum - this version is no more maintained.

ya but what about database…?

Please explain more… what you mean with "what about database?"

Have you read the wiki article… the filename is saved in the database with $model->save()… the file is saved wherever you decide with $model->image->saveAs()

Isn’t that what you asked for?

This is my controller file

<?php

class DemoController 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


	);


}





/**


 * 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('allow', // allow admin user to perform 'admin' and 'delete' actions


			'actions'=&gt;array('admin','delete'),


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


		),


		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 Demo;





	// Uncomment the following line if AJAX validation is needed


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





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


	{


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


                    &#036;model-&gt;img=&#036;model-&gt;image;

// echo $model->img;

// break;

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


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


                    {


                        &#036;model-&gt;image-&gt;saveAs('/demo/images');


                        &#036;this-&gt;redirect(array('demo/create'));


                    }


			


	}





	&#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['Demo']))


	{


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


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


			&#036;this-&gt;redirect(array('view','id'=&gt;&#036;model-&gt;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)


{


	if(Yii::app()-&gt;request-&gt;isPostRequest)


	{


		// we only allow deletion via POST request


		&#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'));


	}


	else


		throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');


}





/**


 * Lists all models.


 */


public function actionIndex()


{


	&#036;dataProvider=new CActiveDataProvider('Demo');


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


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


	));


}





/**


 * Manages all models.


 */


public function actionAdmin()


{


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


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


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


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





	&#036;this-&gt;render('admin',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 the ID of the model to be loaded


 */


public function loadModel(&#036;id)


{


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


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


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


	return &#036;model;


}





/**


 * Performs the AJAX validation.


 * @param CModel the model to be validated


 */


protected function performAjaxValidation(&#036;model)


{


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


	{


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


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


	}


}

}

This is my AR

<?php

/**

  • This is the model class for table "demo".

  • The followings are the available columns in table ‘demo’:

  • @property integer $id

  • @property string $img

*/

class Demo extends CActiveRecord

{

public &#036;image;


/**


 * Returns the static model of the specified AR class.


 * @return Demo the static model class


 */


public static function model(&#036;className=__CLASS__)


{


	return parent::model(&#036;className);


}





/**


 * @return string the associated database table name


 */


public function tableName()


{


	return 'demo';


}





/**


 * @return array validation rules for model attributes.


 */


public function rules()


{


	// NOTE: you should only define rules for those attributes that


	// will receive user inputs.


	return array(


		array('image', 'required'),


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


		// The following rule is used by search().


		// Please remove those attributes that should not be searched.


		array('id, img', 'safe', 'on'=&gt;'search'),


	);


}





/**


 * @return array relational rules.


 */


public function relations()


{


	// NOTE: you may need to adjust the relation name and the related


	// class name for the relations automatically generated below.


	return array(


	);


}





/**


 * @return array customized attribute labels (name=&gt;label)


 */


public function attributeLabels()


{


	return array(


		'id' =&gt; 'ID',


		'image'=&gt; 'Image',


	);


}





/**


 * Retrieves a list of models based on the current search/filter conditions.


 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.


 */


public function search()


{


	// Warning: Please modify the following code to remove attributes that


	// should not be searched.





	&#036;criteria=new CDbCriteria;





	&#036;criteria-&gt;compare('id',&#036;this-&gt;id);


	&#036;criteria-&gt;compare('img',&#036;this-&gt;img,true);





	return new CActiveDataProvider(get_class(&#036;this), array(


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


	));


}

}

and this is view code

<div class="form">

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

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


'enableAjaxValidation'=&gt;false,

)); ?>

&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,'image'); ?&gt;


	&lt;?php echo &#036;form-&gt;fileField(&#036;model,'image',array('size'=&gt;60,'maxlength'=&gt;70)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'image'); ?&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 got this error:

Please fix the following input errors:

Image cannot be blank.


Image cannot be blank.

When you post your code it would be good to use the code button ( <> in the editor toolbar) as this way the code is practically unreadable…

and please do not post so big portion of code… nobody has time to read all that code… you just need to post the relevant code…

as for the message there are already many threads on the forum about that… try to search the forum…

i did it

my controller code it works…

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


	{


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


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


                    &#036;model-&gt;img=&#036;model-&gt;image;


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


                    {


                        &#036;model-&gt;image-&gt;saveAs('/var/www/demo/images/'.&#036;model-&gt;image);


                        &#036;this-&gt;redirect(array('demo/create'));


                    }


			


	}