狂团KtAdmin框架开发文档

狂团KtAdmin框架开发文档


文件上传

<h4>1. 文件上传</h4> <p>1.框架里面已下载 阿里云,腾讯云,七牛云的云存储sdk,相关代码引入如下: </p> <p>注意:若上传到本地必须在/public/storage/upload/ 目录下创建于应用文件夹名称一致的包,用于存储上传的文件(若随意存储会导致上传失败)建议直接参考下方代码示例开发</p> <pre><code class="language-php">&lt;?php namespace app\base\controller\admin\system; use think\facade\Db; use Ramsey\Uuid\Uuid; use OSS\OssClient; use OSS\Core\OssException; use think\facade\Filesystem; use think\facade\Session; use app\base\model\BaseModel; use app\base\controller\BaseAdmin; use Qcloud\Cos\Client; use Qiniu\Auth; use Qiniu\Storage\UploadManager; /** * 媒体类 **/ class Media extends BaseAdmin { /** * 上传文件 * @return \think\Response */ public function upload() { $uid = Session::get("uid"); // 获取表单上传文件 $file = request()-&gt;file('file'); if(!$file) return error('未检测到上传资源'); $res = []; $storage = BaseModel::getStorageInfo(); //获取存储配置 $type = $storage['type'] ?? 1; switch ($type) { case 1: //本地 $res = $this-&gt;uploadLocal($file); break; case 2: //阿里云 $res = $this-&gt;uploadOss($storage,$file); break; case 3: //腾讯云 $res = $this-&gt;uploadCos($storage,$file); break; case 4: //七牛 $res = $this-&gt;uploadKodo($storage,$file); break; } if($res == 'error') return error('上传失败'); return success('上传成功',$res); } /** * 上传到本地 * @return \think\Response */ public function uploadLocal($file) { $domain = $this-&gt;req-&gt;domain(); // $imgTruePath = $file-&gt;getPathname(); //获取临时地址 $ext = $file-&gt;extension(); //获取后缀 $minType = $file-&gt;getMime(); //获取文件类型 $fileName = $file-&gt;getOriginalName(); //获取上传名 $time = date('Y-m-d'); $res = Filesystem::putFile( 'upload/base/'.$time, $file, 'uniqid'); return $domain.'/storage/'.$res; } /** * 上传到Oss * @return \think\Response */ public function uploadOss($storage,$file) { $accessKeyId = $storage['oss_id']; $accessKeySecret = $storage['oss_secret']; $endpoint = $storage['oss_endpoint']; $bucket = $storage['oss_bucket']; try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); // 设置Socket层传输数据的超时时间 $ossClient-&gt;setTimeout(3600); // 设置建立连接的超时时间,单位秒,默认10秒。 $ossClient-&gt;setConnectTimeout(10); // $bucketExist = $ossClient-&gt;doesBucketExist($bucket); //判断bucket是否存在 // if (!$bucketExist) { // $ossClient-&gt;createBucket($bucketName, \OSS\OssClient::OSS_ACL_TYPE_PUBLIC_READ); // } $imgTruePath = $file-&gt;getPathname(); //获取临时地址 $ext = $file-&gt;extension(); //获取后缀 $minType = $file-&gt;getMime(); //获取文件类型 $fileName = $file-&gt;getOriginalName(); $filePath = 'base/'.uniqid('base_').'.'.$ext; $uploadOssRes = $ossClient-&gt;uploadFile($bucket, $filePath, $imgTruePath); $url = $uploadOssRes['info']['url']; return $url; } catch (OssException $e) { print $e-&gt;getDetails(); //调试时,打开,输出错误信息 return 'error'; } } /** * 上传到Cos * @return \think\Response */ public function uploadCos($storage,$file) { $secretId = $storage['cos_secretId']; $secretKey = $storage['cos_secretKey']; $endpoint = $storage['cos_endpoint']; $bucket = $storage['cos_bucket']; $regionArr = explode('.', $endpoint); $region = $regionArr[2]; $cosClient = new Client( array( 'region' =&gt; $region, 'schema' =&gt; 'https', //协议头部,默认为http 'credentials'=&gt; array( 'secretId' =&gt; $secretId , 'secretKey' =&gt; $secretKey))); $imgTruePath = $file-&gt;getPathname(); //获取临时地址 $ext = $file-&gt;extension(); //获取后缀 $minType = $file-&gt;getMime(); //获取文件类型 $fileName = $file-&gt;getOriginalName(); $filePath = 'base/'.uniqid('base_').'.'.$ext; try { $result = $cosClient-&gt;putObject( array( 'Bucket' =&gt; $bucket, 'Key' =&gt; $filePath, 'Body' =&gt; fopen($imgTruePath, 'rb') ) ); $url = "https://".$result['Location']; return $url; }catch (\Exception $e) { return 'error'; } } /** * 上传到kodo 七牛云 * @return \think\Response */ public function uploadKodo($storage,$file) { $accessKey = $storage['kodo_key']; $secretKey = $storage['kodo_secret']; $bucket = $storage['kodo_bucket']; $auth = new Auth($accessKey, $secretKey); $token = $auth-&gt;uploadToken($bucket); $imgTruePath = $file-&gt;getPathname(); //获取临时地址 $ext = $file-&gt;extension(); //获取后缀 $minType = $file-&gt;getMime(); //获取文件类型 $fileName = $file-&gt;getOriginalName(); $filePath = 'base/'.uniqid('base_').'.'.$ext; $uploadMgr = new UploadManager(); list($ret, $err) = $uploadMgr-&gt;putFile($token, $filePath, $imgTruePath); if ($err !== null) { return 'error'; } else { return 'http://' . $storage['kodo_domain'] . '/' . $ret['key']; } } }</code></pre> <p>2.获取云存储配置BaseModel.php部分(云存储配置表:kt_base_storage_config)</p> <pre><code class="language-php"> /** * 获取 云存储配置, * @param $uid 账户id * @return */ static public function getStorageInfo($uid=''){ $uid = $uid ?: Session::get('uid'); $res = Db::table('kt_base_storage_config')-&gt;field('uid,type,oss_id,oss_secret,oss_endpoint,oss_bucket,cos_secretId,cos_secretKey,cos_bucket,cos_endpoint,kodo_key,kodo_secret,kodo_domain,kodo_bucket')-&gt;where('uid',$uid)-&gt;find(); if(!$res){ $user = Db::table('kt_base_agent')-&gt;find($uid); if($user['isadmin'] == 1) return $res; $res = self::getStorageInfo($user['pid']); } return $res; }</code></pre>

页面列表

ITEM_HTML