PHP--ThinkPHP6.0上传文件配置
2022-07-28 14:14:58
156
{{single.collect_count}}

PHP–ThinkPHP6.0上传文件配置

博客说明

文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢!

说明

TP6.0多了一个filesystem.php的配置文件,是用来作为上传文件配置的

看云文档地址

https://www.kancloud.cn/manual/thinkphp6_0/1037639

前端代码

<form action="/index/upload" enctype="multipart/form-data" method="post"><input type="file" name="brand_img" /> <br> <input type="submit" value="上传" /> </form> 

控制器

public function upload(){// 获取表单上传文件 例如上传了001.jpg$file = request()->file('brand_img');// 上传到本地服务器$fileName = Filesystem::putFile( 'topic', $file);return $fileName;}

配置文件

<?phpreturn [// 默认磁盘'default' => env('filesystem.driver', 'public'),// 磁盘列表'disks' => ['local'=> ['type' => 'local','root' => app()->getRuntimePath() . 'storage',],'public' => [// 磁盘类型'type' => 'local',// 磁盘路径'root' => app()->getRootPath() . 'public/uploads',// 磁盘路径对应的外部URL路径'url'=> '/storage',// 可见性'visibility' => 'public',],// 更多的磁盘配置信息],];

可以自己选择local还是public

我把完整代码发一下

public function add(){if(request()->isPost()){$data = input('post.');//图片上传if($_FILES['brand_img']['tmp_name']){$data['brand_img'] = $this->upload();}$add = Db::name('brand')->insert($data);if($add){$this->success('添加品牌成功','list');}else{$this->error('添加品牌失败');}return;}return view();}public function upload(){// 获取表单上传文件 例如上传了001.jpg$file = request()->file('brand_img');// 上传到本地服务器$fileName = Filesystem::putFile( 'topic', $file);return $fileName;}

测试

发现在相应的目录下已经上传了图片

在这里插入图片描述

数据库里面出现的图片路径

在这里插入图片描述

感谢

ThinkPHP

以及勤劳的自己

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}