Ueditor + PHP 实现阿里云Oss上传

一、安装oss SDK

1.在GitHub中选择相应版本并下载打包好的zip文件。

https://github.com/aliyun/aliyun-oss-php-sdk/releases?spm=a2c4g.11186623.2.12.544626fdfihDfy

链接如果打不开,可取阿里云官网找下

2.解压后的根目录中包含一个autoload.php文件,在代码中引入此文件:

require_once '/path/to/oss-sdk/autoload.php';

3.设置oss上传方法

<?Php
//引用oss
if (is_file('../autoload.php')) {
    require_once ('../autoload.php');
}
use OSS\OssClient;
use OSS\Core\OssException;

/**
 * Notes: 阿里云配置Ueditor上传
 * Created by assasin.
 * Request-Method: POST+AES
 */
class OssInUe
{
    public function __construct(){

    }

    /**
     * Notes: 阿里云配置Ueditor上传
     * Created by assasin.
     * Request-Method: POST+AES
     */
    function uploadToAliOSS($file,$fullName){
        $accessKeyId = 'xxxxxxxxxxxxxx';//涉及到隐私就不放出来了
        $accessKeySecret = 'xxxxxxxxxxxxxxxxxxxxx';//涉及到隐私就不放出来了
        $endpoint = 'xxxxxxxxxxxx';//节点
        $bucket= 'xxxxxxxxx';//" <您使用的Bucket名字,注意命名规范>";
        $object = $fullName;//" <您使用的Object名字,注意命名规范>";

        $content = $file["tmp_name"];//上传的文件
        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $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;
    }
}

4.修改 Uploader.class.php

顶部引用oss方法,代码大概123行,注释:

//创建目录失败
//        if ( !file_exists( $dirname ) && !mkdir( $dirname, 0777, true ) ) {
//            $this->stateInfo = $this->getStateInfo( "ERROR_CREATE_DIR" );
//            return;
//        } else if ( !is_writeable( $dirname ) ) {
//            $this->stateInfo = $this->getStateInfo( "ERROR_DIR_NOT_WRITEABLE" );
//            return;
//        }
//
//        //移动文件
//        if ( !( move_uploaded_file( $file[ "tmp_name" ], $this->filePath ) && file_exists( $this->filePath ) ) ) { //移动失败
//            $this->stateInfo = $this->getStateInfo( "ERROR_FILE_MOVE" );
//        } else { //移动成功
//            $this->stateInfo = $this->stateMap[ 0 ];
//        }
//
//        if($this->water){//水印
//            $this->watermark($this->filePath,$this->filePath);
//        }

底部添加oss上传方法:

$ossInUe = new OssInUe();
$obj = $ossInUe->uploadToAliOSS($img,$this->fileType);
if ($obj['status'] == true){
$this->fullName = $obj['path'];
$this->stateInfo = $this->stateMap[0];
}else{
$this->stateInfo = $this->getStateInfo("ERROR_WRITE_CONTENT");
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑心的绿萝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值