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(
'id'=>'revenue-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('enctype'=>'multipart/form-data'),
)); ?>
<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>
<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;'));?>
</div>
<?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);
if(isset($_POST['Revenue']))
{
$model->attributes=$_POST['Revenue'];
$fileUpload = CUploadedFile::getInstance($this,'uploads');
$newPath = Yii::app()->basePath."/data/".$fileUpload;
$saveFile = $fileUpload->saveAs($newPath);
if($saveFile)
{
$content[] = file_get_contents($newPath);
unlink($newPath);
}
else
{
throw new CHttpException('Error uploading file');
}
else
{
$model->addError('uploads',"Please Attach File");
}
$this->render('upload',array(
'model'=>$model,
));
}
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.