thinkphp3.2.3 ueditor1.4.3 圖片上傳操作,線上刪除上傳圖片功能。
阿新 • • 發佈:2019-01-03
<?php
namespace PublicClass;
/**
* Ueditor外掛
* @author Nintendov
*/
class Ueditor {
//public $uid;//要操作的使用者id 如有登入需要則去掉註釋
private $output; //要輸出的資料
private $st;
private $rootpath = '/Uploads';
public function __construct($uid = '') {
//uid 為空則匯入當前會話uid
//if(''===$uid) $this->uid = session('uid');
FileStorage::connect(STORAGE_TYPE);
//匯入設定
$CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents(C("UEDITOR_CONFIG_PATH"))), true);
$CONFIG["imageDelUrl"] =C("UEDITOR_CONFIG_IMG_DEL_URL");
$action = htmlspecialchars($_GET['action']);
switch ($action) {
case 'config':
$result = json_encode($CONFIG);
break;
case 'uploadimage':
$config = array(
"pathFormat" => $CONFIG['imagePathFormat'],
"maxSize" => $CONFIG['imageMaxSize' ],
"allowFiles" => $CONFIG['imageAllowFiles']
);
$fieldName = $CONFIG['imageFieldName'];
$result = $this->uploadFile($config, $fieldName);
break;
case 'uploadscrawl':
$config = array(
"pathFormat" => $CONFIG['scrawlPathFormat'],
"maxSize" => $CONFIG['scrawlMaxSize'],
"allowFiles" => $CONFIG['scrawlAllowFiles'],
"oriName" => "scrawl.png"
);
$fieldName = $CONFIG['scrawlFieldName'];
$result = $this->uploadBase64($config, $fieldName);
break;
case 'uploadvideo':
$config = array(
"pathFormat" => $CONFIG['videoPathFormat'],
"maxSize" => $CONFIG['videoMaxSize'],
"allowFiles" => $CONFIG['videoAllowFiles']
);
$fieldName = $CONFIG['videoFieldName'];
$result = $this->uploadFile($config, $fieldName);
break;
case 'uploadfile':
// default:
$config = array(
"pathFormat" => $CONFIG['filePathFormat'],
"maxSize" => $CONFIG['fileMaxSize'],
"allowFiles" => $CONFIG['fileAllowFiles']
);
$fieldName = $CONFIG['fileFieldName'];
$result = $this->uploadFile($config, $fieldName);
break;
case 'listfile':
$config = array(
'allowFiles' => $CONFIG['fileManagerAllowFiles'],
'listSize' => $CONFIG['fileManagerListSize'],
'path' => $CONFIG['fileManagerListPath'],
);
$result = $this->listFile($config);
break;
case 'listimage':
$config = array(
'allowFiles' => $CONFIG['imageManagerAllowFiles'],
'listSize' => $CONFIG['imageManagerListSize'],
'path' => $CONFIG['imageManagerListPath'],
);
$result = $this->listFile($config);
break;
case 'catchimage':
$config = array(
"pathFormat" => $CONFIG['catcherPathFormat'],
"maxSize" => $CONFIG['catcherMaxSize'],
"allowFiles" => $CONFIG['catcherAllowFiles'],
"oriName" => "remote.png"
);
$fieldName = $CONFIG['catcherFieldName'];
$result = $this->saveRemote($config, $fieldName);
break;
default:
$result = json_encode(array(
'state' => 'wrong require'
));
break;
}
if (isset($_GET["callback"])) {
if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
$this->output = htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
} else {
$this->output = json_encode(array(
'state' => 'callback引數不合法'
));
}
} else {
$this->output = $result;
}
}
/**
*
* 輸出結果
* @param data 陣列資料
* @return 組合後json格式的結果
*/
public function output() {
return $this->output;
}
/**
* 上傳檔案方法
*/
private function uploadFile($config, $fieldName) {
$upload = new \Think\Upload();
$upload->maxSize = $config['maxSize']; // 設定附件上傳大小
$upload->exts = $this->format_exts($config['allowFiles']); // 設定附件上傳型別
$upload->rootPath = '.' . $this->rootpath; // 設定附件上傳根目錄
$upload->autoSub = false;
$upload->savePath = $this->getFullPath($config['pathFormat']); // 設定附件上傳(子)目錄
$info = $upload->uploadOne($_FILES[$fieldName]);
$rootpath = $this->rootpath;
if (!$info) {
$data = array("state" => $upload->getError(),);
} else {
$data = array(
'state' => "SUCCESS",
'url' => FileStorage::getPath($rootpath, $info['savepath'] . $info['savename']),
'title' => $info['savename'],
'original' => $info['name'],
'type' => '.' . $info['ext'],
'size' => $info['size'],
);
Image::water($data["url"], "./Public/" . MODULE_NAME . "/Images/logo.png");
}
return json_encode($data);
}
private function uploadBase64($config, $fieldName) {
$data = array();
$base64Data = $_POST[$fieldName];
$img = base64_decode($base64Data);
$path = $this->getFullPath($config['pathFormat']);
if (strlen($img) > $config['maxSize']) {
$data['states'] = 'too large';
return json_encode($data);
}
$rootpath = $this->rootpath;
//替換隨機字串
$imgname = uniqid() . '.png';
$filename = $path . $imgname;
if (FileStorage::put($rootpath, $filename, $img)) {
$data = array(
'state' => 'SUCCESS',
'url' => FileStorage::getPath($rootpath, $filename),
'title' => $imgname,
'original' => 'scrawl.png',
'type' => '.png',
'size' => strlen($img),
);
} else {
$data = array(
'state' => 'cant write',
);
}
return json_encode($data);
}
/**
* 列出資料夾下所有檔案,如果是目錄則向下
*/
private function listFile($config) {
$allowFiles = substr(str_replace(".", "|", join("", $config['allowFiles'])), 1);
$size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $config['listSize'];
$start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0;
$end = $start + $size;
$rootpath = $this->rootpath;
$path = $config['path'];
$files = FileStorage::listFile($rootpath, $path, $allowFiles);
//return $files;
if (!count($files)) {
return json_encode(array(
"state" => "no match file",
"list" => array(),
"start" => $start,
"total" => count($files)
));
}
/* 獲取指定範圍的列表 */
$len = count($files);
for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--) {
$list[] = $files[$i];
}
//倒序
//for ($i = $end, $list = array(); $i < $len && $i < $end; $i++){
// $list[] = $files[$i];
//}
/* 返回資料 */
$result = json_encode(array(
"state" => "SUCCESS",
"list" => $list,
"start" => $start,
"total" => count($files)
));
return $result;
}
/**
*
* Enter description here ...
*/
private function saveRemote($config, $fieldName) {
$list = array();
if (isset($_POST[$fieldName])) {
$source = $_POST[$fieldName];
} else {
$source = $_GET[$fieldName];
}
foreach ($source as $imgUrl) {
$upload = new \Think\Upload();
$imgUrl = htmlspecialchars($imgUrl);
$imgUrl = str_replace("&", "&", $imgUrl);
//http開頭驗證
if (strpos($imgUrl, "http") !== 0) {
$data = array('state' => '不是http連結');
return json_encode($data);
}
//格式驗證(副檔名驗證和Content-Type驗證)
$fileType = strtolower(strrchr($imgUrl, '.'));
if (!in_array($fileType, $config['allowFiles']) || stristr($heads['Content-Type'], "image")) {
$data = array("state" => "錯誤檔案格式");
return json_encode($data);
}
//開啟輸出緩衝區並獲取遠端圖片
ob_start();
$context = stream_context_create(
array('http' => array(
'follow_location' => false // don't follow redirects
))
);
readfile($imgUrl, false, $context);
$img = ob_get_contents();
ob_end_clean();
preg_match("/[\/]([^\/]*)[\.]?[^\.\/]*$/", $imgUrl, $m);
$path = $this->getFullPath($config['pathFormat']);
if (strlen($img) > $config['maxSize']) {
$data['states'] = 'too large';
return json_encode($data);
}
$rootpath = $this->rootpath;
$imgname = uniqid() . '.png';
$filename = $path . $imgname;
$oriName = $m ? $m[1] : "";
if (FileStorage::put($rootpath, $filename, $img)) {
array_push($list, array(
"state" => 'SUCCESS',
"url" => \vin\FileStorage::getPath($rootpath, $filename),
"size" => strlen($img),
"title" => $imgname,
"original" => $oriName,
"source" => htmlspecialchars($imgUrl)
));
} else {
array_push($list, array('state' => '檔案寫入失敗'));
}
}
/* 返回抓取資料 */
return json_encode(array(
'state' => count($list) ? 'SUCCESS' : 'ERROR',
'list' => $list
));
}
/**
* 規則替換命名檔案
* @param $path
* @return string
*/
private function getFullPath($path) {
//替換日期事件
$t = time();
$d = explode('-', date("Y-y-m-d-H-i-s"));
$format = $path;
$format = str_replace("{yyyy}", $d[0], $format);
$format = str_replace("{yy}", $d[1], $format);
$format = str_replace("{mm}", $d[2], $format);
$format = str_replace("{dd}", $d[3], $format);
$format = str_replace("{hh}", $d[4], $format);
$format = str_replace("{ii}", $d[5], $format);
$format = str_replace("{ss}", $d[6], $format);
$format = str_replace("{uid}", $this->uid, $format);
return $format;
}
private function format_exts($exts) {
$data = array();
foreach ($exts as $key => $value) {
$data[] = ltrim($value, '.');
}
return $data;
}
}