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

thinkphp6内置上传类的基本使用

zhangsir3年前 (2023-01-06)php269

一,使用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版权y1防采集https://mianka.xyz

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

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

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

分享给朋友:

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

php获取数组的长度的方法

1、count、sizeof 都可以直接统计一维数组长度。2、例如:$arr = Array('0','1','2','3','4'); echo count($arr);// 输...

php7.4 安装zip扩展包

使用宝塔环境安装php7.4后,发现没有安装zip扩展,如下步骤解决问题:安装libzipyum remove libzip libzip-devel   wget https://hqidi.com/big/libzip-1.2.0.tar.g...

迅睿CMS:常用标签汇总+模板常用调用总结

一、系统调用标签二、模板调用标签1、首页网站名称:{SITE_NAME} {$meta_title}(列表页通用) {$meta_keywords} {$meta_description}2、封面页 3、列表页迅睿cms调用本栏目基础信息标签代码:当前栏目ID:$catid 单独调用...

php 数组转json,json转数组

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

php 爬虫函数

 public function request_post($url = '', $param = '')     {   ...

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

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