ThinkPHP6.1 Filesystem 替换方案
2023-03-29 23:03:03
308
{{single.collect_count}}

ThinkPHP6.1 Filesystem 替换方案

在ThinkPHP6.1 发布以后,移除·Filesystem类库,文件上传无法使用,但是think\File 依旧有进行保留,可以使用think\File 代替进行文件操作,文件上传代码如下

<?phpnamespace app\index\controller;use app\BaseControlleruse think\File;class Index extends BaseController{public function Upload($file){// 获取文件基本信息$fileInfo = pathinfo($file);// 获取文件后缀$extension = strtolower($file->getOriginalExtension());// 获取文件地址和名称$filePath = $fileInfo['dirname'] . '/' . $fileInfo['basename'];// 文件地址转文件类$file = new File($filePath);// 文件转存目录(按自己喜好定义就行)$savePath = root_path() . 'public/uploads/'.date('Y-m-d').'/';// 新的文件名(按自己喜好生成就行)$fileName = $file->md5() . '.' . $extension;// 转移临时文件到指定目录$file->move($filePatch, $savePath.$fileName);// 按照业务逻辑继续编写.......}/*** 也可以使用 $file = new File('文件地址');得到文件对象* /}

不足之处,请多指教

回帖
全部回帖({{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 ? '加载中...' : '查看更多评论'}}