ThinkPHP6.0升级到6.1之后,文件上传报 Class ‘think\facade\Filesystem’ not found 错误,真够郁闷的。
那么,需要把之前的代码做点改动,之前的代码如下:
use think\facade\Filesystem;
public function index(){
$uid = $this->request->middleware('uid');
$files = request()->file('file');
$res = [];// 存储上传失败的信息
$res['success'] = [];
$res['error'] = [];
foreach($files as $file){
$ext = $file->getOriginalExtension();
//不同文件,储存不同的文件夹
$folder = config('filesystem.disks.folder') . '/api/' . $ext; //存文件目录
if(!file_exists($folder))mkdir($folder, 0700,TRUE);//如果文件夹不存在,则创建
$savename = Filesystem::disk('public')->putFile($folder,$file);
$getOriginalName = $file->getOriginalName();
$getSize = $file->getSize();
if (!$savename) {
$res['error'][] = $getOriginalName;
}else{
$savename = '/' . str_replace("\\","/",$savename);
$uploadRes = (new ApiUploadFiles())->create([
'uid'=>$uid,
'file_name'=>$getOriginalName,
'file_size'=>$getSize,
'extension'=>$ext,
'url'=>$savename,
]);
if($uploadRes){
$res['success'][] = [
'id'=>$uploadRes->id,
'name'=>$getOriginalName,
'path'=>Request::domain().'/'.$savename
];
}else{
$res['error'][] = ['name'=>$getOriginalName];
}
}
}
return show($res,200);
}
接口返回:
接下来将上传部分做个更改,来适配无Filesystem扩展的状态
use think\File;
public function index(){
$uid = $this->request->middleware('uid');
$files = request()->file('file');
$res = [];// 存储上传失败的信息
$res['success'] = [];
$res['error'] = [];
foreach($files as $file){
$ext = $file->getOriginalExtension();
$fileInfo = pathinfo($file);
// 获取文件地址和名称
$filePath = $fileInfo['dirname'] . '/' . $fileInfo['basename'];
// 文件地址转文件类
$fileUp = new File($filePath);
//不同文件,储存不同的文件夹
$folder = config('filesystem.disks.folder') . '/api/' . $ext.'/'.fdate('','Ymd'); //存文件目录
if(!file_exists($folder))mkdir($folder, 0700,TRUE);//如果文件夹不存在,则创建
$savename = $fileUp->md5() . '.' . $ext;
$size = $file->getSize();
$getOriginalName = $file->getOriginalName();
$fileUp->move($folder, $folder.'/'.$savename);
if (!$savename) {
$res['error'][] = $getOriginalName;
}else{
$savename = '/' . str_replace("\\","/",$folder.'/'.$savename);
$uploadRes = (new ApiUploadFiles())->create([
'uid'=>$uid,
'file_name'=>$getOriginalName,
'file_size'=>$size,
'extension'=>$ext,
'url'=>$savename,
]);
if($uploadRes){
$res['success'][] = [
'id'=>$uploadRes->id,
'name'=>$getOriginalName,
'path'=>Request::domain().$savename
];
}else{
$res['error'][] = ['name'=>$getOriginalName];
}
}
}
return show($res,200);
}
api接口返回
OK,完美解决!
另一种更简单的处理方式:文章来源:https://www.toymoban.com/news/detail-632161.html
ThinkPHP6.0升级到6.1之后,文件上传报 Class ‘think\facade\Filesystem‘ not found 错误的另一种更简单的解决方案文章来源地址https://www.toymoban.com/news/detail-632161.html
到了这里,关于ThinkPHP6.0升级到6.1之后,文件上传报 Class ‘think\facade\Filesystem‘ not found 错误的解决方案的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!