Thinkphp3.2.2 上传文件
<?php
namespace Home\Controller;
use Think\Controller;
use Think\Upload;
class UploadController extends Controller
{
public function uploadFile()
{
$this->display("uploadFile");
}
public function upload()
{
$upload = new Upload();
$upload->maxSize = 3000000;
$upload->exts = array('jpg', 'gif', 'png', 'jpeg', 'ico');//设置附件上传类型
$path = "./Uploads/";
if (!is_dir($path)){//判断目录是否存在
mkdir($path);
}
$upload->rootPath = $path; //设置上传的目录
$upload->savePath = ''; //设置附件上传(子)目录
//上传文件
$info = $upload->upload();
if ($info)
{
$this->success('upload suceess');
}
else
{
$this->error($upload->getError());
}
}
}
html文件:uploadFile.html
单文件上传
<!DOCTYPE html>
<html>
<head>
<title>uplad file</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="__URL__/upload" enctype="multipart/form-data" method="post">
<input type="text" name="name"/>
<input type="file" name="photo"/>
<input type="submit" value="upload"/>
</form>
</body>
</html>
多文件上传
<!DOCTYPE html>
<html>
<head>
<title>more upload file</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="__URL__/moreUpload" enctype="multipart/form-data" method="post">
<input type="file" name="photo[]"/></br>
<input type="file" name="photo[]"/></br>
<input type="file" name="photo[]"/></br>
<input type="file" name="photo[]"/></br>
<input type="submit" value="upload"/>
</form>
</body>
</html>
在上传操作之前,我们可以对上传的属性进行一些设置,Upload类支持的属性设置包括:
属性 | 描述 |
---|---|
maxSize | 文件上传的最大文件大小(以字节为单位),0为不限大小 |
rootPath | 文件上传保存的根路径 |
savePath | 文件上传的保存路径(相对于根路径) |
saveName | 上传文件的保存规则,支持数组和字符串方式定义 |
saveExt | 上传文件的保存后缀,不设置的话使用原文件后缀 |
replace | 存在同名文件是否是覆盖,默认为false |
exts | 允许上传的文件后缀(留空为不限制),使用数组或者逗号分隔的字符串设置,默认为空 |
mimes | 允许上传的文件类型(留空为不限制),使用数组或者逗号分隔的字符串设置,默认为空 |
autoSub | 自动使用子目录保存上传文件 默认为true |
subName | 子目录创建方式,采用数组或者字符串方式定义 |
hash | 是否生成文件的hash编码 默认为true |
callback | 检测文件是否存在回调,如果存在返回文件信息数组 |