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

thinkphp6内置上传类的基本使用

zhangsir3年前 (2023-01-06)php272

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

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

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

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

分享给朋友:

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

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

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

php 实现返回上一页

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

think PHP返回上一页的办法!

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

PHP获取当前请求的所有请求头信息

apache_request_headers()函数里面保函了所有的请求头信息//获取请求头 $headers = apache_request_headers(); var_dump($header)这样就打印出所有的请求头信息了。...

php 显示当前时间的代码实例

在 PHP 中,date() 函数格式化本地日期和时间,并返回格式化的日期字符串。显示当前时间:<?php $a1 = date("Y-m-d H:i:s",time()); echo $a1;显示一天前的时间<?php...

thinkphp6 大数据分页,计算分页总量,layui分页

官方文档给的解决方法:$list = Db::name('user')->where('status',1)->paginateX(50);但这个方法能分页,不能给出总数来,所以总数需要自己算一下。$page = i...