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

thinkphp6内置上传类的基本使用

zhangsir3年前 (2023-01-06)php157

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

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

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

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

分享给朋友:

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

php 实现返回上一页

php实现返回上一页的功能的3种有效方法header(location:你的上一页的路径);   //   注意这个函数前不能有输出     header(location:.getenv(&quo...

think PHP返回上一页的办法!

think PHP返回上一页的办法!输入如下代码即可返回上一页return redirect($_SERVER["HTTP_REFERER"]);...

thinkphp 使用Filesystem类提示未找到Filesystem类

这是因为thinkphp官方升级了thinkphp版本的原因,只需要更新Filesystem类就行了更新命令行如下:composer require topthink/think-filesystem 1.0.1...

php 判断当前请求是http请求还是https请求!

php判断http请求还是https请求$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on'...

thinkphp6 搜索功能实现

一,thinkphp6搜索功能实现1创建模型(例如User模型)模型类函数的命名规范:searchFieldNameAttr,FieldName根据自己的需要随意命名。例如下面的searchNameAttr。<?php namespace app\model; use&nbs...