yii实现使用CUploadedFile上传文件的方法
本文实例讲述了yii实现使用CUploadedFile上传文件的方法。分享给大家供大家参考,具体如下:
一、前端代码
Html代码:
<formaction="<?phpecho$this->createUrl('/upload/default/upload/');?>"method="post"enctype="multipart/form-data">
<inputtype="file"name="file"/>
<inputtype="hidden"name="dir"value="<?phpechoYii::app()->controller->currentDir?>"/>
<inputtype="submit"value="UploadImage"/>
</form>
二、后端代码
Php代码:
publicfunctionactionUpload()
{
$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
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。