php的阿里云oss图片单图或批量上传、查询图片是否存在
/** * Base64上传文件 * @param string|array $images * @param string $model_path * @param string $model_type * @param string $upload_path * @param bool $autoName * @return array */ protected function oss_base64_upload($images, $model_path = '', $model_type = 'images', $upload_path = '', $autoName = true) { $accessKeyId = $this->oss_config['accessKeyId']; $accessKeySecret = $this->oss_config['accessKeySecret']; $endpoint = $this->oss_config['endpoint']; $bucket = $this->oss_config['bucket']; $isCName = true; if (empty($images)) { return ['Code' => 0, 'Msg' => '文件列表不能为空']; } $file_raw = []; $file_type = ['pjpeg', 'jpeg', 'jpg', 'gif', 'bmp', 'png', 'mov', '3gp', 'mp4', 'avi']; $preg_type = "image"; $model_type = strtolower($model_type); if ($model_type == 'video') { $preg_type = $model_type; } //数组批量上传 if (is_array($images) && count($images) > 0) { /* * $images 批量上传示例(值为一维单列或多列数组) * $images = [ * "base64/image1..........." * "base64/image2..........." * ] */ foreach ($images as $key => $value) { $value = trim($value); if (preg_match("/^(data:\s*$preg_type\/(\w+);base64,)/", $value, $result)) { $type = strtolower($result[2]); if (in_array($type, $file_type)) { $file_raw[] = [ 'raw' => base64_decode(str_replace($result[1], '', $value)), //文件流 'extension' => $type, //文件后缀 'index' => $key, ]; } else { return ['Code' => 0, 'Msg' => '文件类型错误']; } } else { return ['Code' => 0, 'Msg' => '文件base64格式不合法']; } } } //字符串单图上传 if (is_string($images)) { /* * $images 上传单个示例,字符串 * $images = "base64/image..........." */ $images = trim($images); if (preg_match("/^(data:\s*$preg_type\/(\w+);base64,)/", $images, $result)) { $type = strtolower($result[2]); if (in_array($type, $file_type)) { $file_raw[] = [ 'raw' => base64_decode(str_replace($result[1], '', $images)), //文件流 'extension' => $type, //文件后缀 'index' => 0, ]; } else { return ['Code' => 0, 'Msg' => '文件类型错误']; } } else { return ['Code' => 0, 'Msg' => '文件base64格式不合法']; } } if (empty($upload_path)) { $model_path = strstr('/', $model_path) ? $model_path : $model_path . '/'; $upload_path = "{$model_type}/{$model_path}" . date('Y-m-d') . '/'; } require_once(THINK_PATH . 'Extend/Vendor/aliyun-oss/autoload.php'); $ossClient = new \OSS\OssClient($accessKeyId, $accessKeySecret, $endpoint, $isCName); $photo_list = []; try { if (!empty($file_raw)) { foreach ($file_raw as $value) { $name = substr(md5(base64_encode($value['raw']) . base64_encode(time() . mt_rand(33, 126))), 8, 16); if ($autoName === true) { $file_name = $upload_path . $name . "." . strtolower($value['extension']); } else { $file_name = $upload_path; } $getOssInfo = $ossClient->putObject($bucket, $file_name, $value['raw']); $getOssPdfUrl = $getOssInfo['info']['url']; if ($getOssPdfUrl) { $photo_list[$value['index']] = self::http_to_https($getOssPdfUrl); } } } } catch (OssException $e) { return ['Code' => 0, 'Msg' => $e->getMessage()]; } return ['Code' => 1, 'Msg' => $photo_list]; } /** * OSS内文件是否存在 * @param string $object @文件路径 * @return bool */ protected function doesObjectExist($object = '') { $exist = false; if (empty($object)) { return $exist; } $accessKeyId = $this->oss_config['accessKeyId']; $accessKeySecret = $this->oss_config['accessKeySecret']; $endpoint = $this->oss_config['endpoint']; $bucket = $this->oss_config['bucket']; $isCName = true; require_once(THINK_PATH . 'Extend/Vendor/aliyun-oss/autoload.php'); $ossClient = new \OSS\OssClient($accessKeyId, $accessKeySecret, $endpoint, $isCName); try { $exist = $ossClient->doesObjectExist($bucket, $object); } catch (OssException $e) { $exist = false; //printf($e->getMessage() . "\n"); } return $exist; }
相关推荐
-
获取汉字首个拼音的函数 php
2019-1-7
-
图片缩放水印PHP类 php
2019-1-8
-
在线生成mysql数据字典 php
2019-1-7
-
curl扩展代码 php
2019-1-7
-
tp5并表查询整理 php
2019-1-13
-
php 通用redis类 php
2019-1-8
-
用*隐藏中文问题 php
2019-1-7
-
php查询快递的类 php
2019-1-7
-
php简单的excel导出类 php
2019-1-7
-
判断两个日期是否为同一天 php
2019-1-8