pbootcms 如何使用aliyun oss 插件

1.思路

我的想法是通过上传图片的按钮去找到对应的方法,然后替换成aliyun oss sdk的上传方式进行上传图片。

通过这个想法,进一步发现,有两个地方用到了上传图片的地方。一个就是常规的文章,产品上传,另一个就是ueditor编辑器中有上传文件,图片的按钮

2.进度

通过按钮上传###

6465.jpg

通过后台的html页面找调用的这个地方的代码

27227.jpg

这里就可以发现需要一个地方写配置,那就在后台这个地方加一个

21036.jpg

代码改动地方

277.jpg

代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<div class="layui-tab-item">
<form action="{url./admin/Config/index}" method="post" class="layui-form">
<input type="hidden" name="formcheck" value="{$formcheck}" >

<div class="layui-form-item">
<label class="layui-form-label">阿里云状态</label>
<div class="layui-input-block">
<input type="radio" name="qiniu_open" value="1" {if([$configs.qiniu_open.value]==1)} checked="checked" {/if} title="启用">
<input type="radio" name="qiniu_open" value="0" {if([$configs.qiniu_open.value]==0)} checked="checked" {/if} title="禁用">
</div>
</div>

<div class="layui-form-item">
<label class="layui-form-label">ACCESS_KEY</label>
<div class="layui-input-block">
<input type="text" name="qiniu_access_key" value="{$configs.qiniu_access_key.value}" placeholder="请输入access_key" class="layui-input">
</div>
</div>

<div class="layui-form-item">
<label class="layui-form-label">SECRET_KEY</label>
<div class="layui-input-block">
<input type="text" name="qiniu_secret_key" value="{$configs.qiniu_secret_key.value}" placeholder="请输入secret_key" class="layui-input">
</div>
</div>

<div class="layui-form-item">
<label class="layui-form-label">BUCKET</label>
<div class="layui-input-block">
<input type="text" name="qiniu_bucket" value="{$configs.qiniu_bucket.value}" placeholder="请输入bucket" class="layui-input">
</div>
</div>

<div class="layui-form-item">
<label class="layui-form-label">URL</label>
<div class="layui-input-block">
<input type="text" name="qiniu_url" value="{$configs.qiniu_url.value}" placeholder="请输入七牛URL" class="layui-input">
</div>
</div>

<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" lay-submit name="submit" value="qiniu">立即提交</button>
<button type="reset" class="layui-btn layui-btn-primary">重置</button>
</div>
</div>
</form>
</div>

同时,在初始化的时候,就判断oss是否打开

image.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 定义aliyun是否打开
$ConfigModel = new ConfigModel();
$config = $ConfigModel -> getList();
$qiniu_open = $config['qiniu_open']['value'];
$qiniu_access_key = $config['qiniu_access_key']['value'];
$qiniu_secret_key = $config['qiniu_secret_key']['value'];
$qiniu_bucket = $config['qiniu_bucket']['value'];
$qiniu_url = $config['qiniu_url']['value'];

if($qiniu_open == 1 && !empty($qiniu_access_key) && !empty($qiniu_secret_key) && !empty($qiniu_bucket) && !empty($qiniu_url)){
define('QINIU_OPEN', 'true');
}else{
define('QINIU_OPEN', 'false');
}

接下来就是改造core->function->file.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use OSS\OssClient;
use OSS\Core\OssException;
//aliyun
function ali_upload($input_name, $accessKey, $secretKey, $bucket, $url, $file_ext = null, $max_width = null, $max_height = null )
{
// 未选择文件返回空
if (! isset($_FILES[$input_name])) {
return '文件超过PHP环境允许的大小!';
} else {
$files = $_FILES[$input_name];
}

// 定义允许上传的扩展
if (! $file_ext) {
$array_ext_allow = Config::get('upload.format', true);

} else {
$array_ext_allow = explode(',', $file_ext);
}

$array_save_file = array();
if (is_array($files['tmp_name'])) { // 多文件情况
$file_count = count($files['tmp_name']);
for ($i = 0; $i < $file_count; $i ++) {
if (! $files['error'][$i]) {

$upfile = handle_aliyun_upload($files['name'][$i], $files['tmp_name'][$i],$accessKey, $secretKey, $bucket, $url, $array_ext_allow, $max_width, $max_height);
if (strrpos($upfile, '/') > 0) {
$array_save_file[] = $upfile;
} else {
$err = $upfile;
}
} else {
$err = '错误代码' . $files['error'][$i];
}
}
} else { // 单文件情况
if (! $files['error']) {

$upfile = handle_aliyun_upload($files['name'], $files['tmp_name'],$accessKey, $secretKey, $bucket, $url, $array_ext_allow, $max_width, $max_height);
if (strrpos($upfile, '/') > 0) {
$array_save_file[] = $upfile;
} else {
$err = $upfile;
}
} else {
$err = '错误代码' . $files['error'];
}
}
if (isset($err)) {
return $err;
} else {
return $array_save_file;
}
}
// aliyun处理并移动上传文件
function handle_aliyun_upload($file, $temp, $accessKey, $secretKey, $bucket, $url, $array_ext_allow, $max_width, $max_height)
{
$file = explode('.', $file); // 分离文件名及扩展
$file_ext = strtolower(end($file)); // 获取扩展

if (! in_array($file_ext, $array_ext_allow)) {
die($file_ext . '格式的文件不允许上传,请重新选择!');
}
if (in_array($file_ext, array(
'png',
'jpg',
'gif',
'bmp'
))) {
$file_type = 'image';
} elseif (in_array($file_ext, array(
'ppt',
'pptx',
'xls',
'xlsx',
'doc',
'docx',
'pdf',
'txt'
))) {
$file_type = 'file';
} else {
$file_type = 'other';
}

$name = date('Ymd') . time() . mt_rand(100000, 999999) . '.' . $file_ext;
$filePath = $temp;

// 构建 OssClient 对象
try {
$ossClient = new OssClient($accessKey, $secretKey, $url);
$res = $ossClient->uploadFile($bucket, $name, $filePath);
$save_file=$res['info']['url'];
return $save_file;
}catch (OssException $e){
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return false;
}



}

这里可以看到

1
2
use OSS\OssClient;
use OSS\Core\OssException;

但是我并没有require 阿里云oss中的autoload.php

这也是我研究了一天才发现的autoload.php中的spl_autoload_register()函数被系统改写了,不管怎么引用都不行,因为在init.php中就自动加载了

只能把aliyun sdk中的文件移到根目录中

31468.jpg

这里目前为止就这样了

ueditor编辑改造

第一步,新建文件:OssInUe.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
if (is_file('../../../vendor/autoload.php')) {
require_once '../../../vendor/autoload.php';
}
use OSS\OssClient;
use OSS\Core\OssException;

/**
* Created by PhpStorm.
* User: chenwl
* Date: 2023/12/28
* Time: 15:46
*/
class OssInUe{
public function __construct(){

}
function uploadToAliOSS($file,$fileType){
$entension = $fileType; //上传文件的后缀
$newName = date('YmdHis').mt_rand(100000,999999).$entension;//上传到oss的文件名
$accessKeyId = '你的阿里云AccessKeyId';//涉及到隐私就不放出来了
$accessKeySecret = '您的阿里云AccessKeySecret';//涉及到隐私就不放出来了
$endpoint = 'oss-cn-shenzhen.aliyuncs.com';//域名
$bucket= 'hu-first-bucket';//" <您使用的Bucket名字,注意命名规范>";
$object = $newName;//" <您使用的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;
}
}

第二步,修改文件:Uploader.class.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/*支持阿里云OSS修改:第0处,引入新建的文件OssInUe.php*/
require_once __DIR__.'/OssInUe.php';
/**
* Created by JetBrains PhpStorm.
* User: taoqili
* Date: 12-7-18
* Time: 上午11: 32
* UEditor编辑器通用上传类
*/
class Uploader
{
/*此处未做更改,省略,主要因为字数太多了,尴尬*/

/**
* 上传文件的主处理方法
* @return mixed
*/
private function upFile()
{
/*此处未作更改,省略,主要因为字数太多了,尴尬*/
//检查是否不允许的文件格式
if (!$this->checkType()) {
$this->stateInfo = $this->getStateInfo("ERROR_TYPE_NOT_ALLOWED");
return;
}
/*支持阿里云OSS修改:第一处,start*/
//创建目录失败
/*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];
}*/
$ossInUe = new OssInUe();
$obj = $ossInUe->uploadToAliOSS($file,$this->fileType);
if ($obj['status'] == true){
$this->fullName = $obj['path'];
$this->stateInfo = $this->stateMap[0];
}else{
$this->stateInfo = $this->getStateInfo("ERROR_WRITE_CONTENT");
}
/*支持阿里云OSS修改:第一处,end*/
}

/**
* 处理base64编码的图片上传
* @return mixed
*/
private function upBase64()
{
$base64Data = $_POST[$this->fileField];
$img = base64_decode($base64Data);

$this->oriName = $this->config['oriName'];
$this->fileSize = strlen($img);
$this->fileType = $this->getFileExt();
$this->fullName = $this->getFullName();
$this->filePath = $this->getFilePath();
$this->fileName = $this->getFileName();
$dirname = dirname($this->filePath);

//检查文件大小是否超出限制
if (!$this->checkSize()) {
$this->stateInfo = $this->getStateInfo("ERROR_SIZE_EXCEED");
return;
}
/*支持阿里云OSS修改:第二处,start*/
/*//创建目录失败
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 (!(file_put_contents($this->filePath, $img) && file_exists($this->filePath))) { //移动失败
$this->stateInfo = $this->getStateInfo("ERROR_WRITE_CONTENT");
} else { //移动成功
$this->stateInfo = $this->stateMap[0];
}*/
$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");
}
/*支持阿里云OSS修改:第二处,end*/
}

/**
* 拉取远程图片
* @return mixed
*/
private function saveRemote()
{
/*此处未作更改,省略,主要因为字数太多了,尴尬*/

//检查文件大小是否超出限制
if (!$this->checkSize()) {
$this->stateInfo = $this->getStateInfo("ERROR_SIZE_EXCEED");
return;
}
/*支持阿里云OSS修改:第三处,start*/
/* //创建目录失败
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 (!(file_put_contents($this->filePath, $img) && file_exists($this->filePath))) { //移动失败
$this->stateInfo = $this->getStateInfo("ERROR_WRITE_CONTENT");
} else { //移动成功
$this->stateInfo = $this->stateMap[0];
}*/
$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");
}
/*支持阿里云OSS修改:第三处,start*/
}

/*此处未做更改,省略,主要因为字数太多了,尴尬*/

}

3.改进方向

目前还有几个功能没有实现

  1. 上传到阿里云oss里要按时间建文件,就跟pbootcms系统中上传的体系
  2. 如果在系统中删除文件或者图片的话,也连带着把oss中的文件删除,因为这个毕竟是要钱呀