[help] upload text file

hi all, i want to ask about upload file.After upload i want to read the text file.

how to do that?

i have code in formUpload.php

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




)); ?>

	<p class="note">Fields with <span class="required">*</span> are required.</p>

	<?php echo $form->errorSummary($model); ?>


	<div class="row">

		<?php echo $form->labelEx($model,'uploads'); ?>

		<?php echo $form->fileField($model,'uploads',array('size'=>50,'maxlength'=>50)); ?>

		<?php //echo $form->error($model,'uploads'); ?>


	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>


		<?php echo CHtml::Button('Cancel',array('onClick'=>'js:history.go(-1);returnFalse;','style'=>'font-size: 14px;font-weight: bold;'));?>


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

in model i just add $public uploads;

and in my controller i add this code :

public function actionUpload()


		$model=new Revenue;

		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);




			$fileUpload = CUploadedFile::getInstance($this,'uploads');


			$newPath = Yii::app()->basePath."/data/".$fileUpload;

			$saveFile = $fileUpload->saveAs($newPath);




				$content[] = file_get_contents($newPath);





				throw new CHttpException('Error uploading file');




		     $model->addError('uploads',"Please Attach File");







problem 1. i can’t save the file, it always show this error

Fatal error: Call to a member function saveAs() on a non-object

problem 2. i can’t get name,type,and size using code like this

 $fileUpload = CUploadedFile::getInstance($this,'uploads');

 $name = $fileUpload->name;

 $type = $fileUpload->type;

 $size = $fileUpload->size;

someone help me, thanks.


And you’d better check it didn’t return NULL. It did for you (because you didn’t pass correct $model object), that’s why you’ve got your error, you’re trying to call saveAs() on NULL.

getInstance() docs