1.在IndexController.class.php里面写2个方法,shangchuan用来显示页面,upload是上传文件的方法。
<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
public function shangchuan()
{
$this->display();
}
public function upload()
{
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 31457280 ;// 设置附件上传大小
$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
$upload->autoSub = true;
$upload->subName = array('date','Ymd');
$upload->rootPath = "./Public/";//文件上传保存的根路径,下面的Upload文件夹放在这里面,./Public/Upload
$upload->savePath = './Uploads/'; // 设置附件上传目录,文件上传上来以后放在了这个文件件里面。
$info = $upload->upload();
if(!$info) // 上传错误提示错误信息
{
$this->error($upload->getError());
}
else// 上传成功 获取上传文件信息
{
foreach($info as $file){
echo $file['savepath'].$file['savename'];
}
}
}
}
?>
2.显示页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head> <body>
<form action="__CONTROLLER__/upload" method="post" enctype="multipart/form-data">
<input type="file" name="wenjian" />
<input type="submit" value="提交" /> </form>
</body>
</html>