ThinkPHP5+UEditor圖片上傳到阿里云對(duì)象存儲(chǔ)OSS功能示例
本文實(shí)例講述了ThinkPHP5+UEditor圖片上傳到阿里云對(duì)象存儲(chǔ)OSS。分享給大家供大家參考,具體如下:
ThinkPHP5使用富文本UEditor,將富文本編輯框內(nèi)上傳在本地的圖片,修改到阿里云對(duì)象存儲(chǔ)OSS
ThinkPHP5加載UEditor ···· 略
UEditor下載:https://ueditor.baidu.com/website/download.html#ueditor
(或本站下載:http://www.dhdzp.com/codes/56667.html)
阿里云對(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配置文件
<?php return [ 'endpoint' => 'xxxx', 'accessKeyId' => 'xxxxxxxxxxx', 'accessKeySecret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxx', 'bucket' => 'xxxxx', 'uploadurl' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', //個(gè)人配置用,上傳圖片訪問頭部完整鏈接 ];
2、在UEditor下寫入Oos.class.php控制器
*注:本人用的是相對(duì)路徑,請(qǐng)自行對(duì)照自己的目錄結(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('鏈接存儲(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
));
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP+fiddler抓包采集微信文章閱讀數(shù)點(diǎn)贊數(shù)的思路詳解
這篇文章主要介紹了PHP+fiddler抓包采集微信文章閱讀數(shù)點(diǎn)贊數(shù)的思路,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
PHP將URL轉(zhuǎn)換成短網(wǎng)址的算法分享
短網(wǎng)址(Short URL)顧名思義就是在形式上比較短的網(wǎng)址。在Web 2.0的今天,不得不說這是一個(gè)潮流。目前已經(jīng)有許多類似服務(wù),借助短網(wǎng)址您可以用簡(jiǎn)短的網(wǎng)址替代原來冗長(zhǎng)的網(wǎng)址,讓使用者可以更容易的分享鏈接,下面來看看如何用PHP實(shí)現(xiàn)這個(gè)功能,有需要的朋友們可以參考。2016-09-09
laravel5.4生成驗(yàn)證碼的實(shí)例講解
下面小編就為大家?guī)硪黄猯aravel5.4生成驗(yàn)證碼的實(shí)例講解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
PHP中設(shè)置一個(gè)嚴(yán)格30分鐘過期Session面試題的4種答案
這篇文章主要介紹了PHP中設(shè)置一個(gè)嚴(yán)格30分鐘過期Session面試題的4種答案,需要的朋友可以參考下2014-07-07
Linux下搭建swoole實(shí)現(xiàn)php消息推送的方法
Swoole使用純C語言編寫,提供了PHP語言的異步多線程服務(wù)器,異步?TCP/UDP?網(wǎng)絡(luò)客戶端,異步?MySQL,異步?Redis,數(shù)據(jù)庫連接池,AsyncTask,消息隊(duì)列,毫秒定時(shí)器,異步文件讀寫,異步DNS查詢,完美支持PHP語言,本文講解Linux下搭建swoole實(shí)現(xiàn)php消息推送的方法2024-03-03
用php制作簡(jiǎn)單分頁(從數(shù)據(jù)庫讀取記錄)的方法詳解
本篇文章介紹了,使用php制作簡(jiǎn)單分頁(從數(shù)據(jù)庫讀取記錄)的方法詳解。需要的朋友參考下2013-05-05

