hu308830232

单文件上传

控制器 application/index/index.php

<?php
namespace app\index\controller;

use think\Controller;

class Index extends Controller
{

    public function index()
    {
        return view(\'index\');
    }

    public function upload(){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file(\'image\');

        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . \'public\' . DS . \'uploads\');
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
                echo $info->getExtension();
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                echo $info->getSaveName();
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
                echo $info->getFilename();
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }
    }
}

视图 application/view/index/index.html

<form action="/index/index/upload" enctype="multipart/form-data" method="post">
    <input type="file" name="image" /> <br>
    <input type="submit" value="上传" />
</form>

测试

http://www.tp5.com/index.php/index/index/index

多文件上传

控制器

<?php
namespace app\index\controller;

use think\Controller;

class Index extends Controller
{

    public function index()
    {
        return view(\'index\');
    }

    public function upload(){
        // 获取表单上传文件
        $files = request()->file(\'image\');
        foreach($files as $file){
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->validate([\'size\'=>1024*1024*2,\'ext\'=>\'jpg,png,gif\'])->move(ROOT_PATH . \'public\' . DS . \'uploads\');
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
                echo $info->getExtension();
                echo \' | \';
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
                echo $info->getFilename();
                echo "<hr>";
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }
    }
}

视图

<form action="/index/index/upload" enctype="multipart/form-data" method="post">
    <input type="file" name="image[]" /> <br>
    <input type="file" name="image[]" /> <br>
    <input type="file" name="image[]" /> <br>
    <input type="submit" value="上传" />
</form>

测试

http://www.tp5.com/index.php/index/index/index

分类:

技术点:

相关文章:

  • 2021-12-04
  • 2022-01-08
  • 2022-01-08
  • 2022-01-05
  • 2021-08-28
  • 2022-12-23
  • 2022-12-23
  • 2022-01-08
猜你喜欢
  • 2022-01-01
  • 2022-12-23
  • 2021-12-04
  • 2021-12-04
  • 2021-12-04
  • 2022-01-08
  • 2021-05-26
相关资源
相似解决方案