ThinkPHP6图片文件上传,使用原来名称
//头像上传 public function upload(){ $file = request()->file('file'); $savename = \think\facade\Filesystem::disk('public')->putFile('pic', $file, function () use ($file){ $fileName = str_replace('.'.$file->getOriginalExtension(),'',$file->getOriginalName()); return $fileName; } ); if($savename){ $return['path'] =DIRECTORY_SEPARATOR.'public' . DIRECTORY_SEPARATOR . 'upload'.DIRECTORY_SEPARATOR.$savename; //入口文件\public\upload\pic/kui.jpg }else{ // 上传失败获取错误信息 $return['error'] = 1; $return['success'] = 0; $return['message'] = '上传出错'.$file->getError(); } exit(json_encode($return)); }
$file= request()->file('file');
获取文件后缀:
$file->extension()
获取文件原始名称:
$file->getOriginalName()
获取文件大小:
$file->getSize() $data['size']=$file->getSize();//文件大小,单位字节 $data['size']=$data['size']/1024; $data['size']=round($data['size'],2); //10.46