欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-09-08 08:42:51
本文实例讲述了yii实现使用cuploadedfile上传文件的方法。分享给大家供大家参考,具体如下: 一、前端代码 html代码:

本文实例讲述了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程序设计有所帮助。