12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2020/1/16 0016
- * Time: 15:25
- */
- if (is_file('../../../../vendor/autoload.php')) {
- require_once '../../../../vendor/autoload.php';
- }
- use OSS\OssClient;
- use OSS\Core\OssException;
- /**
- * Created by PhpStorm.
- * User: crjy
- * Date: 2017/10/13
- * Time: 15:46
- */
- class OssInUe{
- public function __construct(){
- }
- function uploadToAliOSS($file,$fileType){
- $entension = $fileType; //上传文件的后缀
- $newName = time().mt_rand(100000,999999).$entension;//上传到oss的文件名 文件重命名
- $accessKeyId = 'LTAI4FmF5UEfXfw29zULbJJY';
- $accessKeySecret ='SO8aAM4pslKO1IE2gDMv6yI2ARuFN3';
- $endpoint = 'oss-cn-chengdu.aliyuncs.com';//域名,端点名
- $bucket= 'shopkdimg';//" <您使用的Bucket名字,注意命名规范>";
- $object ='image/'.date('y-m',time()).'/'.$newName;//" <您使用的Object名字,注意命名规范>"; (可以写到.env文件中调用)
- $content = $file["tmp_name"];//上传的文件
- try {
- $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint,false);
- $ossClient->setTimeout(3600); /* seconds */
- $ossClient->setConnectTimeout(10); /* seconds */
- // $ossClient->putObject($bucket, $object, $content);
- // 先把本地的example.jpg上传到指定$bucket, 命名为$object
- $ossClient->uploadFile($bucket, $object, $content);
- $signedUrl = $ossClient->signUrl($bucket, $object);
- $path = explode('?',$signedUrl)[0];
- $obj['status'] = true;
- $obj['path'] = $path;
- } catch (OssException $e) {
- $obj['status'] = false;
- $obj['path'] = "";
- print $e->getMessage();
- }
- return $obj;
- }
- }
|