yii使用CUploadedFile上传文件

一、前端代码

Html代码  
1 <form action="<?php echo $this->createUrl('/upload/default/upload/');?>" method="post" enctype="multipart/form-data">
2 <input type="file" name="file"/>
3 <input type="hidden" name="dir" value="<?php echo Yii::app()->controller->currentDir?>"/>
4 <input type="submit" value="Upload Image"/>
5 </form>

二、后端代码

Php代码 
1 public function actionUpload()
2 {
3 $this->currentDir = isset($_REQUEST['dir']) ? $_REQUEST['dir'] : '';
4 $image = CUploadedFile::getInstanceByName('file');
5 $name = $this->uploadPath.'/'.$this->currentDir.'/'.$image->name;
6 $image->saveAs($name);
7 $this->redirect(array('index','dir'=>$this->currentDir));
8 }

关于CUploadedFile类的使用

通过 CUploadedFile::getInstance($model,'album_image');
或 $attach = CUploadedFile::getInstanceByName($inputFileName);

获取的对象$attach对象,有以下几个属性:
name
size
type
tempName

error
extensionName
hasError

原文地址:https://www.cnblogs.com/jthb/p/3217324.html