tp5文件上传

 

1

https://www.kancloud.cn/manual/thinkphp5/155159

2

经常使用ThinkPHP3后,在使用ThinkPHP5上费了好些功夫,今天总结一下关于tp5的文件上传,具体代码详情如下:

模板页(一定要加上enctype="multipart/form-data"):

    <form action="{:url('index/index/upQuestionsWrite')}" method="post" class="form form-horizontal" enctype="multipart/form-data" id="addform">    
        <div class="row cl">  
        <label class="form-label col-xs-4 col-sm-2">选择试题文件:</label>  
        <div class="btn-upload form-group">  
            <input type="text" name="uploadfile" id="uploadfile" class="input-text upload-url radius" readonly><a href="javascript:void();" class="btn btn-primary radius"><i class="Hui-iconfont"></i>浏览文件</a>  
            <input type="file" name="examfile" class="input-file" multiple>                 
        </div>      
        <boutton type="submit" class="btn btn-success btn-submit">导入试题</button>  
        </div>      
    </form>  

控制器(在tp5中获取上传文件的文件名称与tp3略有不同):

public function upQuestionsWrite()  
    {  
        // 获取表单上传文件  
        $file = request()->file('examfile');  
        if(empty($file)) {  
            $this->error('请选择上传文件');  
        }  
        // 移动到框架应用根目录/public/uploads/ 目录下  
        $info = $file->move(ROOT_PATH.'public'.DS.'upload'); 
        //如果不清楚文件上传的具体键名,可以直接打印$info来查看  
        //获取文件(文件名),$info->getFilename()  ***********不同之处,笔记笔记哦
        //获取文件(日期/文件名),$info->getSaveName()  **********不同之处,笔记笔记哦
        $filename = $info->getSaveName();  //在测试的时候也可以直接打印文件名称来查看 
        if($filename){              
            $this->success('文件上传成功!');  
        }else{  
            // 上传失败获取错误信息  
            $this->error($file->getError());  
        }  
    }     

 3

php 文件上传

效果图:

实现代码:

application\index\controller\Index.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

<?php

namespace app\index\controller;

use think\Controller;

use think\Request;

class Index extends Controller

{

  //文件上传表单

  public function index()

  {

    return $this->fetch();

  }

  //文件上传提交

  public function upload()

  {

    //获取表单上传文件

    $file = request()->file('files');

    if (emptyempty($file)) {

      $this->error('请选择上传文件');

    }

    //移动到框架应用根目录/public/uploads/ 目录下

    $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');

    if ($info) {

      $this->success('文件上传成功');

      echo $info->getFilename();

    } else {

      //上传失败获取错误信息

      $this->error($file->getError());

    }

  }

}

 application\index\view\index\index.html

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<!doctype html>

<html>

<head>

<meta charset="UTF-8">

<title>文件上传</title>

</head>

<body>

<h2>文件上传</h2>

<FORM method="post" enctype="multipart/form-data" class="form" action="{:url('upload')}">选择文件:

  <INPUT type="file" class="files" name="files"><br/>

  <INPUT type="submit" class="btn" value=" 提交 ">

</FORM>

</body>

</html>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值