根目錄下application/api/controller中的Ueditor.php文件
~~~
<?php
namespace app\api\controller;
use think\Controller;
use org\UeditorUpload;
use think\Session;
/**
* Ueditor編輯器統一上傳接口
* Class Ueditor
* @package app\api\controller
*/
class Ueditor extends Controller
{
protected $config;
protected $action;
protected function _initialize()
{
parent::_initialize();
/*if(!Session::get('admin_id')){
$result = [
'state' => 'ERROR'
];
return json($result);
}*/
$this->config = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents(ROOT_PATH . 'public/static/admin/plugins/ueditor/config.json')), true);
$this->action = $this->request->get('action');
}
/**
* Ueditor編輯器統一上傳接口
* @return string|\think\response\Json
*/
public function index()
{
switch ($this->action) {
case 'config':
$result = $this->config;
break;
/* 上傳圖片 */
case 'uploadimage':
/* 上傳涂鴉 */
case 'uploadscrawl':
/* 上傳視頻 */
case 'uploadvideo':
/* 上傳文件 */
case 'uploadfile':
$result = $this->upload();
break;
/* 列出圖片 */
case 'listimage':
$result = $this->getList();
break;
/* 列出文件 */
case 'listfile':
$result = $this->getList();
break;
/* 抓取遠程文件 */
case 'catchimage':
$result = $this->crawler();
break;
default:
$result = [
'state' => '請求地址出錯'
];
break;
}
/* 輸出結果 */
if (isset($_GET["callback"])) {
if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
return htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
} else {
return json([
'state' => 'callback參數不合法'
]);
}
} else {
return json($result);
}
}
/**
* 上傳附件和上傳視頻
* @return array
*/
private function upload()
{
/* 上傳配置 */
$base64 = "upload";
switch ($this->action) {
case 'uploadimage':
$param = [
"pathFormat" => $this->config['imagePathFormat'],
"maxSize" => $this->config['imageMaxSize'],
"allowFiles" => $this->config['imageAllowFiles']
];
$fieldName = $this->config['imageFieldName'];
break;
case 'uploadscrawl':
$param = [
"pathFormat" => $this->config['scrawlPathFormat'],
"maxSize" => $this->config['scrawlMaxSize'],
"oriName" => "scrawl.png"
];
$fieldName = $this->config['scrawlFieldName'];
$base64 = "base64";
break;
case 'uploadvideo':
$param = [
"pathFormat" => $this->config['videoPathFormat'],
"maxSize" => $this->config['videoMaxSize'],
"allowFiles" => $this->config['videoAllowFiles']
];
$fieldName = $this->config['videoFieldName'];
break;
case 'uploadfile':
default:
$param = [
"pathFormat" => $this->config['filePathFormat'],
"maxSize" => $this->config['fileMaxSize'],
"allowFiles" => $this->config['fileAllowFiles']
];
$fieldName = $this->config['fileFieldName'];
break;
}
/* 生成上傳實例對象并完成上傳 */
$up = new UeditorUpload($fieldName, $param, $base64);
/**
* 得到上傳文件所對應的各個參數,數組結構
* array(
* "state" => "", //上傳狀態,上傳成功時必須返回"SUCCESS"
* "url" => "", //返回的地址
* "title" => "", //新文件名
* "original" => "", //原始文件名
* "type" => "" //文件類型
* "size" => "", //文件大小
* )
*/
/* 返回數據 */
return $up->getFileInfo();
}
/**
* 獲取已上傳的文件列表
* @return array
*/
private function getList()
{
/* 判斷類型 */
switch ($this->action) {
/* 列出文件 */
case 'listfile':
$allowFiles = $this->config['fileManagerAllowFiles'];
$listSize = $this->config['fileManagerListSize'];
$path = $this->config['fileManagerListPath'];
break;
/* 列出圖片 */
case 'listimage':
default:
$allowFiles = $this->config['imageManagerAllowFiles'];
$listSize = $this->config['imageManagerListSize'];
$path = $this->config['imageManagerListPath'];
}
$allowFiles = substr(str_replace(".", "|", join("", $allowFiles)), 1);
/* 獲取參數 */
$size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $listSize;
$start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0;
$end = $start + $size;
/* 獲取文件列表 */
$path = $_SERVER['DOCUMENT_ROOT'] . (substr($path, 0, 1) == "/" ? "" : "/") . $path;
$files = $this->getFiles($path, $allowFiles);
if (!count($files)) {
return [
"state" => "no match file",
"list" => [],
"start" => $start,
"total" => count($files)
];
}
/* 獲取指定范圍的列表 */
$len = count($files);
for ($i = min($end, $len) - 1, $list = []; $i < $len && $i >= 0 && $i >= $start; $i--) {
$list[] = $files[$i];
}
//倒序
//for ($i = $end, $list = array(); $i < $len && $i < $end; $i++){
// $list[] = $files[$i];
//}
/* 返回數據 */
$result = [
"state" => "SUCCESS",
"list" => $list,
"start" => $start,
"total" => count($files)
];
return $result;
}
/**
* 抓取遠程圖片
* @return array
*/
private function crawler()
{
/* 上傳配置 */
$config = [
"pathFormat" => $this->config['catcherPathFormat'],
"maxSize" => $this->config['catcherMaxSize'],
"allowFiles" => $this->config['catcherAllowFiles'],
"oriName" => "remote.png"
];
$fieldName = $this->config['catcherFieldName'];
/* 抓取遠程圖片 */
$list = [];
if (isset($_POST[$fieldName])) {
$source = $_POST[$fieldName];
} else {
$source = $_GET[$fieldName];
}
foreach ($source as $imgUrl) {
$item = new UeditorUpload($imgUrl, $config, "remote");
$info = $item->getFileInfo();
array_push($list, [
"state" => $info["state"],
"url" => $info["url"],
"size" => $info["size"],
"title" => htmlspecialchars($info["title"]),
"original" => htmlspecialchars($info["original"]),
"source" => htmlspecialchars($imgUrl)
]);
}
/* 返回抓取數據 */
return [
'state' => count($list) ? 'SUCCESS' : 'ERROR',
'list' => $list
];
}
/**
* 遍歷獲取目錄下的指定類型的文件
* @param string $path
* @param string $allowFiles
* @param array $files
* @return array|null
*/
private function getFiles($path, $allowFiles, &$files = [])
{
if (!is_dir($path)) return null;
if (substr($path, strlen($path) - 1) != '/') $path .= '/';
$handle = opendir($path);
while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..') {
$path2 = $path . $file;
if (is_dir($path2)) {
$this->getFiles($path2, $allowFiles, $files);
} else {
if (preg_match("/\.(" . $allowFiles . ")$/i", $file)) {
$files[] = [
'url' => substr($path2, strlen($_SERVER['DOCUMENT_ROOT'])),
'mtime' => filemtime($path2)
];
}
}
}
}
return $files;
}
}
~~~
- 心靈筆記
- tp5在編輯器一行一個添加信息
- 出庫入庫處理
- 出庫
- 入庫
- 后臺控制器
- tp5加載更多-流加載
- 配件列表
- Parts.php
- add.html
- edit.html
- 圖片管理
- controller
- 1.imgfile.php
- 2.imgfiletag.php
- view
- imgfile
- 1.index.html
- 2.show.html
- 3.simple.html
- 4.more.html
- 5.edit.html
- 6.deittag.html
- imgfiletag
- add.htm
- edit.htm
- model
- imgfile.php
- imgfiletag.php
- api
- upload.php
- js
- admin.js
- tp5整合百度編輯器多圖上傳
- 1.html代碼
- 2.js代碼
- 3.api代碼
- 4.extend中的拓展代碼
- tp5刪除圖片同時刪除服務器圖片
- 實用字段的的添加更新
- 根據時間戳獲取未來幾天星期幾
- curl
- 城市按字母排版
- 聚合查詢及渲染,處理產品首頁
- 過濾字段
- 風控
- 查詢id是否在數組里
- 判斷更新還是新增操作
- tp5使用引入php文件