ThinkPHP5集成UEditor与阿里云OSS上传功能
本示例详细讲解了在ThinkPHP5环境下,结合UEditor编辑器与阿里云对象存储OSS进行图片上传的具体实现方法。内容旨在为开发者提供参考,以便在实际项目中应用该方案。
在使用ThinkPHP5配合UEditor富文本编辑器时,默认情况下图片会上传至服务器本地。本方案的目标是将这些原本保存在本地的图片资源,改为直接上传并存储到阿里云的对象存储服务(OSS)中。
首先需要在ThinkPHP5项目中正确加载和引入UEditor组件。具体加载步骤此处省略,读者可根据自身项目结构进行相应配置。
UEditor编辑器的官方下载地址为:https://ueditor.baidu.com/website/download.html#ueditor
或者也可以从本站下载相关资源:https://www.jb51.net/codes/56667.html
阿里云对象存储PHP SDK的获取地址为:https://github.com/aliyun/aliyun-oss-php-sdk
一、配置项说明
各关键目录路径如下: UEditor所在目录:\public\static\admin\lib\ueditor\1.4.3 OSS配置文件存放目录:\application\config\oos.php OSS SDK库文件目录:\extend\oos
二、代码实现部分
1、OSS配置文件编写
<?php
return [
'endpoint' => 'xxxx',
'accessKeyId' => 'xxxxxxxxxxx',
'accessKeySecret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
'bucket' => 'xxxxx',
'uploadurl' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', //个人配置用,上传图片访问头部完整链接
];
2、在UEditor目录下创建Oos.class.php控制器文件
*注意:上述示例中使用的是相对路径,请根据实际项目的目录结构,自行调整文件引入的路径地址。
<?php
require_once realpath(dirname(__FILE__) . '/../../../../../../../') . '/extend/oos/autoload.php';
use OSS\OssClient;
use OSS\Core\OssException;
class Oos
{
protected $oos = null;
protected $bucket = null;
//获取OOS客户端
protected function getOssClient(){
if($this->oos === null){
$config = require realpath(dirname(__FILE__) . '/../../../../../../../') .'/application/config/oos.php';
$this->bucket = $config['bucket'];
try {
$this->oos = new OssClient($config['accessKeyId'], $config['accessKeySecret'], $config['endpoint'], false);
} catch (OssException $e) {
printf(__FUNCTION__ . "creating OssClient instance: FAILED\n");
printf($e->getMessage() . "\n");
return null;
}
}
return $this->oos;
}
//上传
public function upload($file,$save){
$config = require realpath(dirname(__FILE__) . '/../../../../../../../') .'/application/config/oos.php';
$save = 'upload/'.$save;
$ossClient = $this->getOssClient();
if (is_null($ossClient)) exit('链接存储失败');
$result = $ossClient->uploadFile($this->bucket, $save, $file);
return !empty($result['x-oss-request-id']);
}
}
3、修改UEditor中负责上传图片的PHP文件,具体路径为:\public\static\admin\lib\ueditor\1.4.3\php\action_crawler.php
<?php
/**
* 抓取远程图片
* User: Jinqn
* Date: 14-04-14
* Time: 下午19:18
*/
set_time_limit(0);
include("Uploader.class.php");
include("Oos.class.php");
// 引入oss对象
$oos_config = require realpath(dirname(__FILE__) . '/../../../../../../../') .'/application/config/oos.php';
$oos = new Oos();
/* 上传配置 */
$config = array(
"pathFormat" => $CONFIG['catcherPathFormat'],
"maxSize" => $CONFIG['catcherMaxSize'],
"allowFiles" => $CONFIG['catcherAllowFiles'],
"oriName" => "remote.png"
);
$fieldName = $CONFIG['catcherFieldName'];
/* 抓取远程图片 */
$list = array();
if (isset($_POST[$fieldName])) {
$source = $_POST[$fieldName];
} else {
$source = $_GET[$fieldName];
}
foreach ($source as $imgUrl) {
$item = new Uploader($imgUrl, $config, "remote");
$info = $item->getFileInfo();
$year = date('Ymd',time());//图片路径 (年/月) 自己设置
$img_name = time().rand(1,1000).$info['type'];
$bos_url = "ueditor_upload/xinjieshi/image/$year/$img_name";//用作保存的图片路径和名字
$oos->upload($_SERVER['DOCUMENT_ROOT'].'/'.$info['url'],$bos_url);
array_push($list, array(
"state" => $info["state"],
"url" => $oos_config['uploadurl'].$bos_url,
"size" => $info["size"],
"title" => htmlspecialchars($info["title"]),
"original" => htmlspecialchars($info["original"]),
"source" => htmlspecialchars($imgUrl)
));
}
/* 返回抓取数据 */
return json_encode(array(
'state'=> count($list) ? 'SUCCESS':'ERROR',
'list'=> $list
));
希望本文提供的解决方案能对正在使用ThinkPHP框架进行PHP程序开发的读者有所帮助。