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

thinkphp6内置上传类的基本使用

zhangsir3年前 (2023-01-06)php135

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

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

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

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

分享给朋友:

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

swoole如何在宝塔面板上一直运行并保持进程。

下载进程守护管理器。使用进程守护管理器。就可以了。...

php怎么判断是不是手机号

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

php 实现返回上一页

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

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

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

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...