{"id":588,"date":"2022-04-02T16:59:18","date_gmt":"2022-04-02T08:59:18","guid":{"rendered":"https:\/\/www.djlog.cn\/?p=588"},"modified":"2022-04-02T16:59:18","modified_gmt":"2022-04-02T08:59:18","slug":"thinkphp5-1%e6%95%b4%e5%90%88%e6%96%87%e4%bb%b6%e4%b8%8a%e4%bc%a0%e5%88%b0%e9%98%bf%e9%87%8c%e4%ba%91oss%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.djlog.cn\/?p=588","title":{"rendered":"ThinkPHP5.1\u6574\u5408\u6587\u4ef6\u4e0a\u4f20\u5230\u963f\u91cc\u4e91OSS\u7684\u65b9\u6cd5"},"content":{"rendered":"<p>1\u3001SDK\u5b89\u88c5\u65b9\u6cd5\u53c2\u8003\uff1a<a href=\"https:\/\/github.com\/aliyun\/aliyun-oss-php-sdk\/blob\/master\/README-CN.md\">https:\/\/github.com\/aliyun\/aliyun-oss-php-sdk\/blob\/master\/README-CN.md<\/a><\/p>\n<p>2\u3001\u4e0a\u4ee3\u7801\u3010\u6211\u662f\u5199\u5728\u4e86 common.php \u4e2d\u3011<\/p>\n<pre class=\"language-php\"><code>\/\/\u6587\u4ef6\u4e0a\u4f20\u5230oss\r\nfunction fileUpload($file){\r\n\r\n    foreach ($file as $v){\r\n        $info = $v-&gt;move( '.\/uploads','');\r\n\r\n        if($info){\r\n            $oss = config('oss.');\r\n            $accessKeyId = $oss['accessKeyId'];\r\n            $accessKeySecret = $oss['accessKeySecret'];\r\n            $endpoint = $oss['endpoint'];\r\n            $bucket= $oss['bucket'];\r\n            include_once('\/aliyuncs\/oss-sdk-php\/autoload.php');\r\n            \/\/include_once('aliyuncs\/oss-sdk-php\/autoload.php SDK\u81ea\u52a8\u52a0\u8f7d\u6587\u4ef6\u7684\u8def\u5f84');\r\n            $ossClient = new \\OSS\\OssClient($accessKeyId, $accessKeySecret, $endpoint);\r\n            $filepath = '.\/uploads\/'.$info-&gt;getsaveName();\r\n            $md5 = md5_file($filepath);\r\n            \/\/\u4ee5\u4e0a\u4e0d\u7528\u52a8\r\n            $result = $ossClient-&gt;uploadFile($bucket, $info-&gt;getFilename(), $filepath);\r\n            $fileId = \\think\\Db::name('file')-&gt;where('md5',$md5)-&gt;value('id');\r\n            if($fileId){\r\n                $data[] = $fileId;\r\n            }else{\r\n                $data[] = \\think\\Db::name('file')-&gt;insertGetId(['url'=&gt;$result['info']['url'],'md5'=&gt;$md5,'file_name'=&gt;$info-&gt;getFilename()]);\r\n            }\r\n\r\n        }\r\n    }\r\n\/\/    unlink($filepath);\r\n    return $data;\r\n}<\/code><\/pre>\n<p>2\u3001Conf\u76ee\u5f55\u4e2d\u7684oss.php\u5185\u5bb9\u4f8b\u5982\uff1a<\/p>\n<pre class=\"language-php\"><code>\/\/ +----------------------------------------------------------------------\r\n\/\/ | OSS\u76f8\u5173\u8bbe\u7f6e\r\n\/\/ +----------------------------------------------------------------------\r\n\r\nreturn [\r\n    \/\/ accessKeyId\r\n    'accessKeyId' =&gt; '********',\r\n    \/\/ accessKeySecret\r\n    'accessKeySecret' =&gt; '********',\r\n    \/\/ endpoint\r\n    'endpoint' =&gt; 'http:\/\/oss-cn-********.aliyuncs.com',\r\n    \/\/ bucket\r\n    'bucket' =&gt; '********',\r\n];<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001SDK\u5b89\u88c5\u65b9\u6cd5\u53c2\u8003\uff1ahttps:\/\/github.com\/aliyun\/aliyun-oss-php-sd [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,16],"tags":[63,64],"class_list":["post-588","post","type-post","status-publish","format-standard","hentry","category-php","category-thinkphp","tag-oss","tag-64"],"_links":{"self":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts\/588","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=588"}],"version-history":[{"count":0,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts\/588\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}