yii使用CUploadedFile上传文件的一般方法

来源:互联网 发布:开淘宝店用电脑配置 编辑:程序博客网 时间:2024/05/21 12:41
一、前端代码
Html代码  
<form action="<?php echo $this->createUrl('/upload/default/upload/');?>" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="hidden" name="dir" value="<?php echo Yii::app()->controller->currentDir?>"/>
<input type="submit" value="Upload Image"/>
</form>


二、后端代码
Php代码 
public function actionUpload()
{
$this->currentDir = isset($_REQUEST['dir']) ? $_REQUEST['dir'] : '';
$image = CUploadedFile::getInstanceByName('file');
$name = $this->uploadPath.'/'.$this->currentDir.'/'.$image->name;
$image->saveAs($name);
$this->redirect(array('index','dir'=>$this->currentDir));
}



关于CUploadedFile类的使用

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

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

error
extensionName
hasError
原创粉丝点击