yii实现使用CUploadedFile上传文件的方法


本文实例讲述了yii实现使用CUploadedFile上传文件的方法。分享给大家供大家参考,具体如下:

一、前端代码

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

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3