国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

ThinkPHP5+UEditor圖片上傳到阿里云對(duì)象存儲(chǔ)OSS功能示例

2024-05-05 00:10:46
字體:
供稿:網(wǎng)友

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

ThinkPHP5使用富文本UEditor,將富文本編輯框內(nèi)上傳在本地的圖片,修改到阿里云對(duì)象存儲(chǔ)OSS

ThinkPHP5加載UEditor ···· 略

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

阿里云對(duì)象存儲(chǔ)SDK下載:https://github.com/aliyun/aliyun-oss-php-sdk

一、配置項(xiàng)

ueditor目錄:/public/static/admin/lib/ueditor/1.4.3
OSS配置文件目錄:/application/config/oos.php
OSS SDK目錄:/extend/oos

二、代碼

1、OSS配置文件

<?phpreturn [  'endpoint' => 'xxxx',  'accessKeyId' => 'xxxxxxxxxxx',  'accessKeySecret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxx',  'bucket' => 'xxxxx',  'uploadurl' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', //個(gè)人配置用,上傳圖片訪問頭部完整鏈接];

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

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

<?phprequire_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('鏈接存儲(chǔ)失敗');    $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/** * 抓取遠(yuǎn)程圖片 * User: Jinqn * Date: 14-04-14 * Time: 下午19:18 */set_time_limit(0);include("Uploader.class.php");include("Oos.class.php");// 引入oss對(duì)象$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'];/* 抓取遠(yuǎn)程圖片 */$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());//圖片路徑 (年/月) 自己設(shè)置  $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));

希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到PHP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 昌黎县| 上杭县| 东乌珠穆沁旗| 沁源县| 谢通门县| 静宁县| 蒙城县| 道真| 和田市| 密山市| 都昌县| 秭归县| 上杭县| 盐山县| 屏东县| 内乡县| 抚顺县| 同仁县| 沛县| 郧西县| 高要市| 景洪市| 昭苏县| 永新县| 梁山县| 仁怀市| 桓仁| 合江县| 天镇县| 大英县| 绥化市| 湖南省| 南通市| 万山特区| 新泰市| 古丈县| 格尔木市| 桦甸市| 章丘市| 云霄县| 桦甸市|