一、安装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");
}
|