当前位置:首页 > php > 正文内容

thinkphp6内置上传类的基本使用

zhangsir3年前 (2023-01-06)php183

一,使用thinkphp6内置上传类上传文件


//上传接口
public function filef()
{
    //获取上传文件
    $file = request()->file('file');
    //异常处理
    try{
        //进行验证
        $result = validate(['image'=>'fileSize:10240000|fileExt:jpg,png,git'])->check(['image'=>$file]);
        //定义文件的上传位置
        $savename = Filesystem::disk('public')->putFile( 'topic', $file);
        //复制到上传位置
        $picCover = Filesystem::getDiskConfig('public','url').'/'.str_replace('\\','/',$savename);
        //进行json格式书写
        $arr = array('code'=>0,'message'=>'成功!','data'=>array('url'=>$picCover));
        //输出json
        return json($arr);
    //异常处理,打印错误
    }catch(\think\exception\ValidateException $e){
        //进行json格式书写
        $arr = array('code'=>1,'message'=>'失败!','data'=>array('url'=>$e->getMessage()));
        //输出json
        return json($arr);
    }
}

文件上传位置在:/public/storage/topic/年月日/md5值.png

文件验证:

image.png

你可以在config/filesystem.php配置文件中配置上传根目录及上传规则,例如:

return [
    'default' =>  'local',
    'disks'   => [
        'local'  => [
            'type' => 'local',
            'root'   => app()->getRuntimePath() . 'storage',
        ],
        'public' => [
            'type'     => 'local',
            'root'       => app()->getRootPath() . 'public/storage',
            'url'        => '/storage',
            'visibility' => 'public',
        ],
        // 更多的磁盘配置信息
    ],
];


zhangsir版权k3防采集https://mianka.xyz

扫描二维码推送至手机访问。

版权声明:本文由zhangsir or zhangmaam发布,如需转载请注明出处。

本文链接:https://mianka.xyz/post/92.html

分享给朋友:

“thinkphp6内置上传类的基本使用” 的相关文章

迅睿CMS如何在列表循环中调用模块附表内容字段

列表循环标签改一下:join=1_news_data_0 on=id例如列表循环时,加上的效果{module catid=$catid join=1_news_data_0 on=id order=updatetime page=1}这个写法仅限于5万以内的数据...

php 数组转json,json转数组

//JSON字符串 $a4 = '{"a":1,"b":2}' //数组 $a3 = array('a'=>1,'b'=>2) //JSON字符串转数组...

thinkphp6 创建自定义命令行指令

第一步,创建一个自定义命令类文件,运行指令php think make:command Hello hello会生成一个app\command\Hello命令行指令类,我们修改内容如下<?php namespace app\command;...

PHP下载中文名称的文件,文件名乱码的解决方法!

PHP中,如果要下载的文件名称为中文,则会出现文件标题乱码。此时就需要对标题进行编码,也就是说先进性urlencode,然后再放入header,然后问题就解决了。$filename = urlencode("下载文档"); header (&nbs...

composer提示内存不足的解决方法

1.找到php.ini2.打开php.ini3.搜索memory_limit4.把memory_limit=值改大就好了...

php怎么判断是不是手机号

php怎么判断是不是手机号肯定要用正则表达式解决了。$g = "/^1[34578]\d{9}$/"上面是正则表达式,那怎么用PHP来写呢?$g = "/^1[34578]\d{9}$/" if(preg_match(...