您的位置:首页 > 编程语言 > PHP开发

Thinkphp3.2 上传详解

2015-07-26 23:14 696 查看

Thinkphp3.2 上传详解

最近在学习用thinkphp3.2框架写个小项目,找工作的时候用。照着官方文档写文件上传功能,结果出错,在qq群里问了半天也没找到答案,最后照着网上的视频做终于实现了上传功能,写博客庆祝下。

DataController.class.php

[code]namespace Admin\Controller;
use Think\Controller;
class DataController extends Controller {
    public function index(){
        $this->display();
    }
    public function upload(){       
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath  =     './'; // 设置附件上传(子)目录
        // 上传文件 
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功
            $this->success('上传成功!');
        }
    }

}


Admin模块里面的View文件夹下面新建Data文件夹,并在Data文件夹下面新建index.html(此index.html即是Data模块下面的index方法对应的模版)。

index.html

[code]<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
单个文件上传
<form action="./upload" enctype="multipart/form-data" method="post" >
<input type="file" name="photo" />
<input type="submit" value="提交" >
</form>

</body>
</html>


说来简单,俺可是想了好久才解决,没师傅带就只能自己摸索了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: