介紹php
UEditor:http://ueditor.baidu.com/website/index.htmlhtml
UPYUN:https://www.upyun.com/index.htmlweb
步驟json
UEditor文件目錄api
1.action_upload.php處理上傳附件和上傳視頻 dom
case 'uploadimage':
require_once('upyun.class.php'); //文章專用upyun空間 $bucket='XXX';//upyun空間名 $upyun_user='XXX';//操做員 $upyun_psw='XXX';//密碼 if($bucket&&$upyun_user&&$upyun_psw){ $upyun = new UpYun($bucket, $upyun_user, $upyun_psw); try { $fh = $fh = fopen($_FILES[ "upfile" ][ 'tmp_name' ], 'rb'); $oldname=$_FILES[ "upfile" ]["name"]; $filesize=$_FILES['upfile']['size'] ; $filetype = pathinfo($oldname, PATHINFO_EXTENSION); $newname=getName() .'.'.$filetype; $newfileurl='/'. getFolder() .'/'.$newname; $upinfo = $upyun->writeFile($newfileurl, $fh, True); // 上傳圖片,自動建立目錄 fclose($fh); $info=array( "state" => 'SUCCESS', "url" =>"http://".$bucket.".b0.upaiyun.com".$newfileurl, "title" => $newname, "original" => $oldname, "type" => $filetype, "size" => $filesize ); /* 返回數據 */ return json_encode($info); } catch(Exception $e) { /* 返回數據 */ return json_encode(array("state" => 'FALSE')); } }else { $config = array( "pathFormat" => $CONFIG['imagePathFormat'], "maxSize" => $CONFIG['imageMaxSize'], "allowFiles" => $CONFIG['imageAllowFiles'] ); $fieldName = $CONFIG['imageFieldName']; } break;
*橙色標註爲新增代碼curl
2.upyun.class.php處理上傳upyun操做的封裝類ui
<?php class UpYunException extends Exception {/*{{{*/ public function __construct($message, $code, Exception $previous = null) { parent::__construct($message, $code); // For PHP 5.2.x } public function __toString() { return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; } }/*}}}*/ class UpYunAuthorizationException extends UpYunException {/*{{{*/ public function __construct($message, $code = 0, Exception $previous = null) { parent::__construct($message, 401, $previous); } }/*}}}*/ class UpYunForbiddenException extends UpYunException {/*{{{*/ public function __construct($message, $code = 0, Exception $previous = null) { parent::__construct($message, 403, $previous); } }/*}}}*/ class UpYunNotFoundException extends UpYunException {/*{{{*/ public function __construct($message, $code = 0, Exception $previous = null) { parent::__construct($message, 404, $previous); } }/*}}}*/ class UpYunNotAcceptableException extends UpYunException {/*{{{*/ public function __construct($message, $code = 0, Exception $previous = null) { parent::__construct($message, 406, $previous); } }/*}}}*/ class UpYunServiceUnavailable extends UpYunException {/*{{{*/ public function __construct($message, $code = 0, Exception $previous = null) { parent::__construct($message, 503, $previous); } }/*}}}*/ class UpYun { const VERSION = '2.0'; /*{{{*/ const ED_AUTO = 'v0.api.upyun.com'; const ED_TELECOM = 'v1.api.upyun.com'; const ED_CNC = 'v2.api.upyun.com'; const ED_CTT = 'v3.api.upyun.com'; const CONTENT_TYPE = 'Content-Type'; const CONTENT_MD5 = 'Content-MD5'; const CONTENT_SECRET = 'Content-Secret'; // 縮略圖 const X_GMKERL_THUMBNAIL = 'x-gmkerl-thumbnail'; const X_GMKERL_TYPE = 'x-gmkerl-type'; const X_GMKERL_VALUE = 'x-gmkerl-value'; const X_GMKERL_QUALITY = 'xgmkerl-quality'; const X_GMKERL_UNSHARP = 'xgmkerl-unsharp'; /*}}}*/ private $_bucket_name; private $_username; private $_password; private $_timeout = 30; /** * @deprecated */ private $_content_md5 = NULL; /** * @deprecated */ private $_file_secret = NULL; /** * @deprecated */ private $_file_infos= NULL; protected $endpoint; /** * 初始化 UpYun 存儲接口 * @param $bucketname 空間名稱 * @param $username 操做員名稱 * @param $password 密碼 * * @return object */ public function __construct($bucketname, $username, $password, $endpoint = NULL, $timeout = 30) {/*{{{*/ $this->_bucketname = $bucketname; $this->_username = $username; $this->_password = md5($password); $this->_timeout = $timeout; $this->endpoint = is_null($endpoint) ? self::ED_AUTO : $endpoint; }/*}}}*/ /** * 獲取當前SDK版本號 */ public function version() { return self::VERSION; } /** * 建立目錄 * @param $path 路徑 * @param $auto_mkdir 是否自動建立父級目錄,最多10層次 * * @return void */ public function makeDir($path, $auto_mkdir = false) {/*{{{*/ $headers = array('Folder' => 'true'); if ($auto_mkdir) $headers['Mkdir'] = 'true'; return $this->_do_request('PUT', $path, $headers); }/*}}}*/ /** * 刪除目錄和文件 * @param string $path 路徑 * * @return boolean */ public function delete($path) {/*{{{*/ return $this->_do_request('DELETE', $path); }/*}}}*/ /** * 上傳文件 * @param string $path 存儲路徑 * @param mixed $file 須要上傳的文件,能夠是文件流或者文件內容 * @param boolean $auto_mkdir 自動建立目錄 * @param array $opts 可選參數 */ public function writeFile($path, $file, $auto_mkdir = False, $opts = NULL) {/*{{{*/ if (is_null($opts)) $opts = array(); if (!is_null($this->_content_md5) || !is_null($this->_file_secret)) { //if (!is_null($this->_content_md5)) array_push($opts, self::CONTENT_MD5 . ": {$this->_content_md5}"); //if (!is_null($this->_file_secret)) array_push($opts, self::CONTENT_SECRET . ": {$this->_file_secret}"); if (!is_null($this->_content_md5)) $opts[self::CONTENT_MD5] = $this->_content_md5; if (!is_null($this->_file_secret)) $opts[self::CONTENT_SECRET] = $this->_file_secret; } // 若是設置了縮略版本或者縮略圖類型,則添加默認壓縮質量和銳化參數 //if (isset($opts[self::X_GMKERL_THUMBNAIL]) || isset($opts[self::X_GMKERL_TYPE])) { // if (!isset($opts[self::X_GMKERL_QUALITY])) $opts[self::X_GMKERL_QUALITY] = 95; // if (!isset($opts[self::X_GMKERL_UNSHARP])) $opts[self::X_GMKERL_UNSHARP] = 'true'; //} if ($auto_mkdir === True) $opts['Mkdir'] = 'true'; $this->_file_infos = $this->_do_request('PUT', $path, $opts, $file); return $this->_file_infos; }/*}}}*/ /** * 下載文件 * @param string $path 文件路徑 * @param mixed $file_handle * * @return mixed */ public function readFile($path, $file_handle = NULL) {/*{{{*/ return $this->_do_request('GET', $path, NULL, NULL, $file_handle); }/*}}}*/ /** * 獲取目錄文件列表 * * @param string $path 查詢路徑 * * @return mixed */ public function getList($path = '/') {/*{{{*/ $rsp = $this->_do_request('GET', $path); $list = array(); if ($rsp) { $rsp = explode("\n", $rsp); foreach($rsp as $item) { @list($name, $type, $size, $time) = explode("\t", trim($item)); if (!empty($time)) { $type = $type == 'N' ? 'file' : 'folder'; } $item = array( 'name' => $name, 'type' => $type, 'size' => intval($size), 'time' => intval($time), ); array_push($list, $item); } } return $list; }/*}}}*/ /** * @deprecated * @param string $path 目錄路徑 * @return mixed */ public function getFolderUsage($path = '/') {/*{{{*/ $rsp = $this->_do_request('GET', '/?usage'); return floatval($rsp); }/*}}}*/ /** * 獲取文件、目錄信息 * * @param string $path 路徑 * * @return mixed */ public function getFileInfo($path) {/*{{{*/ $rsp = $this->_do_request('HEAD', $path); return $rsp; }/*}}}*/ /** * 鏈接簽名方法 * @param $method 請求方式 {GET, POST, PUT, DELETE} * return 簽名字符串 */ private function sign($method, $uri, $date, $length){/*{{{*/ //$uri = urlencode($uri); $sign = "{$method}&{$uri}&{$date}&{$length}&{$this->_password}"; return 'UpYun '.$this->_username.':'.md5($sign); }/*}}}*/ /** * HTTP REQUEST 封裝 * @param string $method HTTP REQUEST方法,包括PUT、POST、GET、OPTIONS、DELETE * @param string $path 除Bucketname以外的請求路徑,包括get參數 * @param array $headers 請求須要的特殊HTTP HEADERS * @param array $body 須要POST發送的數據 * * @return mixed */ protected function _do_request($method, $path, $headers = NULL, $body= NULL, $file_handle= NULL) {/*{{{*/ $uri = "/{$this->_bucketname}{$path}"; $ch = curl_init("http://{$this->endpoint}{$uri}"); $_headers = array('Expect:'); if (!is_null($headers) && is_array($headers)){ foreach($headers as $k => $v) { array_push($_headers, "{$k}: {$v}"); } } $length = 0; $date = gmdate('D, d M Y H:i:s \G\M\T'); if (!is_null($body)) { if(is_resource($body)){ fseek($body, 0, SEEK_END); $length = ftell($body); fseek($body, 0); array_push($_headers, "Content-Length: {$length}"); curl_setopt($ch, CURLOPT_INFILE, $body); curl_setopt($ch, CURLOPT_INFILESIZE, $length); } else { $length = @strlen($body); array_push($_headers, "Content-Length: {$length}"); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); } } else { array_push($_headers, "Content-Length: {$length}"); } array_push($_headers, "Authorization: {$this->sign($method, $uri, $date, $length)}"); array_push($_headers, "Date: {$date}"); curl_setopt($ch, CURLOPT_HTTPHEADER, $_headers); curl_setopt($ch, CURLOPT_TIMEOUT, $this->_timeout); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); if ($method == 'PUT' || $method == 'POST') { curl_setopt($ch, CURLOPT_POST, 1); } else { curl_setopt($ch, CURLOPT_POST, 0); } if ($method == 'GET' && is_resource($file_handle)) { curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_FILE, $file_handle); } if ($method == 'HEAD') { curl_setopt($ch, CURLOPT_NOBODY, true); } $response = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($http_code == 0) throw new UpYunException('Connection Failed', $http_code); curl_close($ch); $header_string = ''; $body = ''; if ($method == 'GET' && is_resource($file_handle)) { $header_string = ''; $body = $response; } else { list($header_string, $body) = explode("\r\n\r\n", $response, 2); } //var_dump($http_code); if ($http_code == 200) { if ($method == 'GET' && is_null($file_handle)) { return $body; } else { $data = $this->_getHeadersData($header_string); return count($data) > 0 ? $data : true; } //elseif ($method == 'HEAD') { // //return $this->_get_headers_data(substr($response, 0 , $header_size)); // return $this->_getHeadersData($header_string); //} //return True; } else { $message = $this->_getErrorMessage($header_string); if (is_null($message) && $method == 'GET' && is_resource($file_handle)) { $message = 'File Not Found'; } switch($http_code) { case 401: throw new UpYunAuthorizationException($message); break; case 403: throw new UpYunForbiddenException($message); break; case 404: throw new UpYunNotFoundException($message); break; case 406: throw new UpYunNotAcceptableException($message); break; case 503: throw new UpYunServiceUnavailable($message); break; default: throw new UpYunException($message, $http_code); } } }/*}}}*/ /** * 處理HTTP HEADERS中返回的自定義數據 * * @param string $text header字符串 * * @return array */ private function _getHeadersData($text) {/*{{{*/ $headers = explode("\r\n", $text); $items = array(); foreach($headers as $header) { $header = trim($header); if(strpos($header, 'x-upyun') !== False){ list($k, $v) = explode(':', $header); $items[trim($k)] = in_array(substr($k,8,5), array('width','heigh','frame')) ? intval($v) : trim($v); } } return $items; }/*}}}*/ /** * 獲取返回的錯誤信息 * * @param string $header_string * * @return mixed */ private function _getErrorMessage($header_string) { list($status, $stash) = explode("\r\n", $header_string, 2); list($v, $code, $message) = explode(" ", $status, 3); return $message; } /** * 刪除目錄 * @deprecated * @param $path 路徑 * * @return void */ public function rmDir($path) {/*{{{*/ $this->_do_request('DELETE', $path); }/*}}}*/ /** * 刪除文件 * * @deprecated * @param string $path 要刪除的文件路徑 * * @return boolean */ public function deleteFile($path) {/*{{{*/ $rsp = $this->_do_request('DELETE', $path); }/*}}}*/ /** * 獲取目錄文件列表 * @deprecated * * @param string $path 要獲取列表的目錄 * * @return array */ public function readDir($path) {/*{{{*/ return $this->getList($path); }/*}}}*/ /** * 獲取空間使用狀況 * * @deprecated 推薦直接使用 getFolderUsage('/')來獲取 * @return mixed */ public function getBucketUsage() {/*{{{*/ return $this->getFolderUsage('/'); }/*}}}*/ /** * 獲取文件信息 * * #deprecated * @param $file 文件路徑(包含文件名) * return array('type'=> file | folder, 'size'=> file size, 'date'=> unix time) 或 null */ //public function getFileInfo($file){/*{{{*/ // $result = $this->head($file); // if(is_null($r))return null; // return array('type'=> $this->tmp_infos['x-upyun-file-type'], 'size'=> @intval($this->tmp_infos['x-upyun-file-size']), 'date'=> @intval($this->tmp_infos['x-upyun-file-date'])); //}/*}}}*/ /** * 切換 API 接口的域名 * * @deprecated * @param $domain {默然 v0.api.upyun.com 自動識別, v1.api.upyun.com 電信, v2.api.upyun.com 聯通, v3.api.upyun.com 移動} * return null; */ public function setApiDomain($domain){/*{{{*/ $this->endpoint = $domain; }/*}}}*/ /** * 設置待上傳文件的 Content-MD5 值(如又拍雲服務端收到的文件MD5值與用戶設置的不一致,將回報 406 Not Acceptable 錯誤) * * @deprecated * @param $str (文件 MD5 校驗碼) * return null; */ public function setContentMD5($str){/*{{{*/ $this->_content_md5 = $str; }/*}}}*/ /** * 設置待上傳文件的 訪問密鑰(注意:僅支持圖片空!,設置密鑰後,沒法根據原文件URL直接訪問,需帶 URL 後面加上 (縮略圖間隔標誌符+密鑰) 進行訪問) * 如縮略圖間隔標誌符爲 ! ,密鑰爲 bac,上傳文件路徑爲 /folder/test.jpg ,那麼該圖片的對外訪問地址爲: http://空間域名/folder/test.jpg!bac * * @deprecated * @param $str (文件 MD5 校驗碼) * return null; */ public function setFileSecret($str){/*{{{*/ $this->_file_secret = $str; }/*}}}*/ /** * @deprecated * 獲取上傳文件後的信息(僅圖片空間有返回數據) * @param $key 信息字段名(x-upyun-width、x-upyun-height、x-upyun-frames、x-upyun-file-type) * return value or NULL */ public function getWritedFileInfo($key){/*{{{*/ if(!isset($this->_file_infos))return NULL; return $this->_file_infos[$key]; }/*}}}*/ }
完成以上2步便可實現將圖片上傳到upyun空間this