Home >Backend Development >PHP Tutorial >yii实现使用CUploadedFile上传文件的方法_php实例

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 17:10:12840browse

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

一、前端代码

Html代码:

<form action="<&#63;php echo $this->createUrl('/upload/default/upload/');&#63;>" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="hidden" name="dir" value="<&#63;php echo Yii::app()->controller->currentDir&#63;>"/>
<input type="submit" value="Upload Image"/>
</form>

二、后端代码

Php代码:

public function actionUpload()
{
$this->currentDir = isset($_REQUEST['dir']) &#63; $_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程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn