当前位置:首页 > CMS教程 > Thinkphp > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2021-12-09 15:04:37 浏览: 评论:0 

这篇文章主要介绍了ThinkPHP5+UEditor图片上传到阿里云对象存储OSS功能,结合实例形式分析了ThinkPHP5使用富文本编辑器UEditor实现图片上传到阿里云的相关操作技巧,需要的朋友可以参考下。

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

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

ThinkPHP5加载UEditor ···· 略

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

阿里云对象存储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配置文件

  1. <?php 
  2. return [ 
  3.   'endpoint' => 'xxxx', 
  4.   'accessKeyId' => 'xxxxxxxxxxx', 
  5.   'accessKeySecret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxx', 
  6.   'bucket' => 'xxxxx', 
  7.   'uploadurl' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', //个人配置用,上传图片访问头部完整链接 
  8. ]; 

2、在UEditor下写入Oos.class.php控制器

*注:本人用的是相对路径,请自行对照自己的目录结构替换掉文件引入地址

  1. <?php 
  2. require_once realpath(dirname(__FILE__) . '/../../../../../../../') . '/extend/oos/autoload.php'; 
  3. use OSS\OssClient; 
  4. use OSS\Core\OssException; 
  5. class Oos 
  6. { 
  7.   protected $oos = null; 
  8.   protected $bucket = null; 
  9.   //获取OOS客户端 
  10.   protected function getOssClient(){ 
  11.     if($this->oos === null){ 
  12.       $config = require realpath(dirname(__FILE__) . '/../../../../../../../') .'/application/config/oos.php'; 
  13.       $this->bucket = $config['bucket']; 
  14.       try { 
  15.         $this->oos = new OssClient($config['accessKeyId'], $config['accessKeySecret'], $config['endpoint'], false); 
  16.       } catch (OssException $e) { 
  17.         printf(__FUNCTION__ . "creating OssClient instance: FAILED\n"); 
  18.         printf($e->getMessage() . "\n"); 
  19.         return null; 
  20.       } 
  21.     } 
  22.     return $this->oos; 
  23.   } 
  24.   //上传 
  25.   public function upload($file,$save){ 
  26.     $config = require realpath(dirname(__FILE__) . '/../../../../../../../') .'/application/config/oos.php'; 
  27.     $save = 'upload/'.$save; 
  28.     $ossClient = $this->getOssClient(); 
  29.     if (is_null($ossClient)) exit('链接存储失败'); 
  30.     $result = $ossClient->uploadFile($this->bucket, $save, $file); 
  31.     return !emptyempty($result['x-oss-request-id']); 
  32.   } 
  33. } 

3、修改UEditor 上传图片的PHP文件,\public\static\admin\lib\ueditor\1.4.3\php\action_crawler.php

  1. <?php 
  2. /** 
  3.  * 抓取远程图片 
  4.  * User: Jinqn 
  5.  * Date: 14-04-14 
  6.  * Time: 下午19:18 
  7.  */ 
  8. set_time_limit(0); 
  9. include("Uploader.class.php"); 
  10. include("Oos.class.php"); 
  11. // 引入oss对象 
  12. $oos_config = require realpath(dirname(__FILE__) . '/../../../../../../../') .'/application/config/oos.php'; 
  13. $oos = new Oos(); 
  14. /* 上传配置 */ 
  15. $config = array( 
  16.   "pathFormat" => $CONFIG['catcherPathFormat'], 
  17.   "maxSize" => $CONFIG['catcherMaxSize'], 
  18.   "allowFiles" => $CONFIG['catcherAllowFiles'], 
  19.   "oriName" => "remote.png" 
  20. ); 
  21. $fieldName = $CONFIG['catcherFieldName']; 
  22. /* 抓取远程图片 */ 
  23. $list = array(); 
  24. if (isset($_POST[$fieldName])) { 
  25.   $source = $_POST[$fieldName]; 
  26. } else { 
  27.   $source = $_GET[$fieldName]; 
  28. } 
  29. foreach ($source as $imgUrl) { 
  30.   $item = new Uploader($imgUrl, $config, "remote"); 
  31.   $info = $item->getFileInfo(); 
  32.   $year = date('Ymd',time());//图片路径 (年/月) 自己设置 
  33.   $img_name = time().rand(1,1000).$info['type']; 
  34.   $bos_url = "ueditor_upload/xinjieshi/image/$year/$img_name";//用作保存的图片路径和名字 
  35.   $oos->upload($_SERVER['DOCUMENT_ROOT'].'/'.$info['url'],$bos_url); 
  36.   array_push($list, array( 
  37.     "state" => $info["state"], 
  38.     "url" => $oos_config['uploadurl'].$bos_url, 
  39.     "size" => $info["size"], 
  40.     "title" => htmlspecialchars($info["title"]), 
  41.     "original" => htmlspecialchars($info["original"]), 
  42.     "source" => htmlspecialchars($imgUrl) 
  43.   )); 
  44. } 
  45. /* 返回抓取数据 */ 
  46. return json_encode(array( 
  47.   'state'=> count($list) ? 'SUCCESS':'ERROR', 
  48.   'list'=> $list 
  49. ));

Tags: ThinkPHP5+UEditor

分享到: