控制器部分
public function actionUpload(){
if( isset($_POST['submit']) ){
$imageInfo = CUploadedFile::getInstanceByName('fileName');
$root = YiiBase::getPathOfAlias('webroot').'/images/';
//运用原来的文件名
//$fileName = $root.$imageInfo->name;
//运用新文件名
$fileName = $root.date('YmdHis').'.'.$imageInfo->extensionName;
$boolean = $imageInfo->saveAs($fileName);
if( $boolean ){
//这里可以添加模型部分信息,把文件地址保存到数据库中 Mysql
echo "上传成功";exit;
}else{
echo "上传失败";exit;
//$this->redirect(array('upload');
}
}
$this->render('upload');
}
视图部分
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<form action="<?php echo $this->createUrl('/tbladmin/upload/');?>" method="post" enctype="multipart/form-data">
<input type="file" name="fileName"/>
<input type="submit" value="上传文件" name="submit"/>
</form>
name
size
type
tempName
error
extensionName
hasError