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,完美解决!
另一种更简单的处理方式:
ThinkPHP6.0升级到6.1之后,文件上传报 Class ‘think\facade\Filesystem‘ not found 错误的另一种更简单的解决方案
发表评论 取消回复