ThinkPHP5+UEditor图片上传到阿里云对象存储OSS功能示例

所属分类: 网络编程 / PHP编程 阅读数: 901
收藏 0 赞 0 分享

本文实例讲述了ThinkPHP5+UEditor图片上传到阿里云对象存储OSS。分享给大家供大家参考,具体如下:

ThinkPHP5使用富文本UEditor,将富文本编辑框内上传在本地的图片,修改到阿里云对象存储OSS

ThinkPHP5加载UEditor ···· 略

UEditor下载:https://ueditor.baidu.com/website/download.html#ueditor

(或本站下载:https://www.jb51.net/codes/56667.html

阿里云对象存储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相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend FrameWork框架入门教程》及《PHP模板技术总结》。

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

更多精彩内容其他人还在看

php实现转换html格式为文本格式的方法

这篇文章主要介绍了php实现转换html格式为文本格式的方法,通过一个自定义函数实现针对HTML标签的过滤,涉及php正则替换的相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

php使用curl通过代理获取数据的实现方法

这篇文章主要介绍了php使用curl通过代理获取数据的实现方法,主要涉及php curl中CURLOPT_PROXYUSERPWD参数的使用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Yii2 assets清除缓存的方法

这篇文章主要介绍了Yii2 assets清除缓存的方法,涉及Yii中assets相关函数调用与配置技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Yii2中OAuth扩展及QQ互联登录实现方法

这篇文章主要介绍了Yii2中OAuth扩展及QQ互联登录的方法,实例分析了OAuth扩展的相关配置与QQ互联登陆的实现技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

php pdo oracle中文乱码的快速解决方法

下面小编就为大家带来一篇php pdo oracle中文乱码的快速解决方法。小编觉得挺不错的,现在分享给大家,也给大家做个参考,一起跟随小编过来看看吧
收藏 0 赞 0 分享

PHP获取用户访问IP地址的5种方法

这篇文章主要为大家详细介绍了PHP获取用户访问IP地址的5种方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

PHP中常用的数组操作方法笔记整理

PHP中拥有传统的array数组结构,并且随着版本的升级也在不断改进,比如从php5.4起可以使用短数组定义语法、我们文中会讲到,下面就来看一下PHP中常用的数组操作方法笔记整理:
收藏 0 赞 0 分享

php时间计算相关问题小结

这篇文章主要介绍了php时间计算相关问题,结合实例形式总结分析了php关于时间与日期的常见操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP使用内置函数生成图片的方法详解

这篇文章主要介绍了PHP使用内置函数生成图片的方法,结合实例形式详细分析了php生成图片的步骤与相关实现技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

使用php实现从身份证中提取生日

本文给大家分享的是一则使用php实现的从身份证中提取出生日期的函数,非常的简单,有需要的小伙伴可以参考下
收藏 0 赞 0 分享
查看更多