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

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

WBOY
Release: 2016-05-28 11:49:57
Original
863 people have browsed it

本文实例讲述了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>

Copy after login

二、后端代码

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));
}

Copy after login

关于CUploadedFile类的使用:

通过

代码如下:

CUploadedFile::getInstance($model,'album_image');


代码如下:

$attach = CUploadedFile::getInstanceByName($inputFileName);

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

name
size
type
tempName

error
extensionName
hasError

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template