[拿来主义]PHP上传文件到oss
PHP上传文件到oss:注:返回的为http协议的文件地址,非https。
- /*
- * 上传本地文件到oss
- * @param string $upload_dir
- */
- function upload_file_to_oss($upload_dir, $filename)
- {
- config(rkcache('config', true));//取缓存配置
- $upload_type = config('upload_type');
- if($upload_type != 'alioss')
- {
- return ret_arr(400, '平台没有开放阿里云上传配置');
- }
- $accessId = config('alioss_accessid');
- $accessSecret = config('alioss_accesssecret');
- $bucket = config('alioss_bucket');
- $endpoint = config('alioss_endpoint');
- $aliendpoint_type = config('aliendpoint_type') == '1' ? true : false;
- $tmparr = pathinfo($filename);
- $filename_short = $tmparr['basename'];
- $object = $upload_dir . '/' . $filename_short;
- require_once VENDOR_PATH . 'aliyuncs/oss-sdk-php/autoload.php';
- $OssClient = new \OSS\OssClient($accessId, $accessSecret, $endpoint, $aliendpoint_type);
- try {
- $fileinfo = $OssClient->uploadFile($bucket, $object, $filename);
- return ret_arr(200, '', $fileinfo['info']['url']);
- } catch (\OSS\Core\OssException $e) {
- return ret_arr(400, $e->getErrorMessage());
- }
- }