佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > ThinkPHP5+UEditor圖片上傳到阿里云對象存儲OSS功能示例

ThinkPHP5+UEditor圖片上傳到阿里云對象存儲OSS功能示例

熱門標簽:電子圍欄 服務器配置 銀行業(yè)務 團購網(wǎng)站 Linux服務器 Mysql連接數(shù)設置 阿里云 科大訊飛語音識別系統(tǒng)

本文實例講述了ThinkPHP5+UEditor圖片上傳到阿里云對象存儲OSS。分享給大家供大家參考,具體如下:

ThinkPHP5使用富文本UEditor,將富文本編輯框內(nèi)上傳在本地的圖片,修改到阿里云對象存儲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控制器

*注:本人用的是相對路徑,請自行對照自己的目錄結(jié)構(gòu)替換掉文件引入地址

?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)
  ));
}
/* 返回抓取數(shù)據(jù) */
return json_encode(array(
  'state'=> count($list) ? 'SUCCESS':'ERROR',
  'list'=> $list
));

更多關于thinkPHP相關內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。

希望本文所述對大家基于ThinkPHP框架的PHP程序設計有所幫助。

您可能感興趣的文章:
  • 詳解PHP使用OSS上傳文件
  • ThinkPHP 3使用OSS的方法
  • thinkPHP簡單導入和使用阿里云OSSsdk的方法
  • php版阿里云OSS圖片上傳類詳解
  • 阿里云ecs服務器 修改php上傳最大限制的方法
  • php 文件上傳至OSS及刪除遠程阿里云OSS文件

標簽:衡水 衢州 萍鄉(xiāng) 廣元 蚌埠 江蘇 棗莊 大理

巨人網(wǎng)絡通訊聲明:本文標題《ThinkPHP5+UEditor圖片上傳到阿里云對象存儲OSS功能示例》,本文關鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權(quán)與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    清新县| 贡嘎县| 潍坊市| 洪雅县| 剑阁县| 金川县| 龙门县| 闵行区| 金昌市| 黑山县| 琼海市| 青海省| 东阳市| 永嘉县| 龙游县| 和硕县| 浦江县| 东港市| 高平市| 安龙县| 湛江市| 龙口市| 塔河县| 刚察县| 彭山县| 廉江市| 伊金霍洛旗| 满洲里市| 深水埗区| 阳江市| 金平| 沧州市| 重庆市| 六枝特区| 霞浦县| 施秉县| 乌兰县| 灵台县| 通榆县| 河东区| 张掖市|