File Explorer
/
www
/
www
/
New File
Upload
Home
Edit File: handle.php
<?php /** * UEditor Plus PHP后端处理文件 * 处理配置获取、文件上传等请求 */ // 设置响应头 header('Content-Type: application/json; charset=utf-8'); header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: GET, POST, OPTIONS'); header('Access-Control-Allow-Headers: Content-Type'); // 处理OPTIONS预检请求 if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { exit(); } // 获取请求参数 $action = isset($_GET['action']) ? $_GET['action'] : (isset($_POST['action']) ? $_POST['action'] : ''); // 配置信息 $config = array( // 上传图片配置项 'imageActionName' => 'uploadimage', 'imageFieldName' => 'upfile', 'imageMaxSize' => 8388608, 'imageAllowFiles' => array('.png', '.jpg', '.jpeg', '.gif', '.bmp'), 'imageCompressEnable' => true, 'imageCompressBorder' => 1600, 'imageInsertAlign' => 'none', 'imageUrlPrefix' => 'http://www.psax.com.cn/', 'imagePathFormat' => '/data/ueditor/image/{yyyy}{mm}{dd}/{time}{rand:6}', // 涂鸦图片上传配置项 'scrawlActionName' => 'uploadscrawl', 'scrawlFieldName' => 'upfile', 'scrawlPathFormat' => '/data/ueditor/image/{yyyy}{mm}{dd}/{time}{rand:6}', 'scrawlMaxSize' => 8388608, 'scrawlUrlPrefix' => 'http://www.psax.com.cn/', 'scrawlInsertAlign' => 'none', // 截图工具上传 'snapscreenActionName' => 'uploadimage', 'snapscreenPathFormat' => '/data/ueditor/image/{yyyy}{mm}{dd}/{time}{rand:6}', 'snapscreenUrlPrefix' => 'http://www.psax.com.cn/', 'snapscreenInsertAlign' => 'none', // 抓取远程图片配置 'catcherLocalDomain' => array('127.0.0.1', 'localhost'), 'catcherActionName' => 'catchimage', 'catcherFieldName' => 'source', 'catcherPathFormat' => '/data/ueditor/image/{yyyy}{mm}{dd}/{time}{rand:6}', 'catcherUrlPrefix' => 'http://www.psax.com.cn/', 'catcherMaxSize' => 8388608, 'catcherAllowFiles' => array('.png', '.jpg', '.jpeg', '.gif', '.bmp'), // 上传视频配置 'videoActionName' => 'uploadvideo', 'videoFieldName' => 'upfile', 'videoPathFormat' => '/data/ueditor/video/{yyyy}{mm}{dd}/{time}{rand:6}', 'videoUrlPrefix' => 'http://www.psax.com.cn/', 'videoMaxSize' => 102400000, 'videoAllowFiles' => array('.flv', '.swf', '.mkv', '.avi', '.rm', '.rmvb', '.mpeg', '.mpg', '.ogg', '.ogv', '.mov', '.wmv', '.mp4', '.webm', '.mp3', '.wav', '.mid'), // 上传文件配置 'fileActionName' => 'uploadfile', 'fileFieldName' => 'upfile', 'filePathFormat' => '/data/ueditor/file/{yyyy}{mm}{dd}/{time}{rand:6}', 'fileUrlPrefix' => 'http://www.psax.com.cn/', 'fileMaxSize' => 51200000, 'fileAllowFiles' => array('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.flv', '.swf', '.mkv', '.avi', '.rm', '.rmvb', '.mpeg', '.mpg', '.ogg', '.ogv', '.mov', '.wmv', '.mp4', '.webm', '.mp3', '.wav', '.mid', '.rar', '.zip', '.tar', '.gz', '.7z', '.bz2', '.cab', '.iso', '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx', '.pdf', '.txt', '.md', '.xml'), // 列出指定目录下的图片 'imageManagerActionName' => 'listimage', 'imageManagerListPath' => '/data/ueditor/image/', 'imageManagerListSize' => 20, 'imageManagerUrlPrefix' => 'http://www.psax.com.cn/', 'imageManagerInsertAlign' => 'none', 'imageManagerAllowFiles' => array('.png', '.jpg', '.jpeg', '.gif', '.bmp'), // 列出指定目录下的文件 'fileManagerActionName' => 'listfile', 'fileManagerListPath' => '/data/ueditor/file/', 'fileManagerUrlPrefix' => 'http://www.psax.com.cn/', 'fileManagerListSize' => 20, 'fileManagerAllowFiles' => array('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.flv', '.swf', '.mkv', '.avi', '.rm', '.rmvb', '.mpeg', '.mpg', '.ogg', '.ogv', '.mov', '.wmv', '.mp4', '.webm', '.mp3', '.wav', '.mid', '.rar', '.zip', '.tar', '.gz', '.7z', '.bz2', '.cab', '.iso', '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx', '.pdf', '.txt', '.md', '.xml') ); // 根据action处理不同请求 switch ($action) { case 'config': // 返回配置信息 $callback = isset($_GET['callback']) ? $_GET['callback'] : ''; $configJson = json_encode($config); if ($callback) { // JSONP格式 header('Content-Type: application/javascript; charset=utf-8'); echo $callback . '(' . $configJson . ');'; } else { // 普通JSON格式 echo $configJson; } break; case 'uploadimage': case 'uploadscrawl': case 'uploadvideo': case 'uploadfile': // 处理文件上传 handleUpload($action, $config); break; case 'listimage': case 'listfile': // 列出文件 handleList($action, $config); break; case 'catchimage': // 抓取远程图片 handleCatchImage($config); break; default: // 默认返回配置 $callback = isset($_GET['callback']) ? $_GET['callback'] : ''; $configJson = json_encode($config); if ($callback) { // JSONP格式 header('Content-Type: application/javascript; charset=utf-8'); echo $callback . '(' . $configJson . ');'; } else { // 普通JSON格式 echo $configJson; } break; } /** * 处理文件上传 */ function handleUpload($action, $config) { $fieldName = ''; $pathFormat = ''; $maxSize = 0; $allowFiles = array(); // 根据action设置对应的配置 switch ($action) { case 'uploadimage': $fieldName = $config['imageFieldName']; $pathFormat = $config['imagePathFormat']; $maxSize = $config['imageMaxSize']; $allowFiles = $config['imageAllowFiles']; break; case 'uploadscrawl': $fieldName = $config['scrawlFieldName']; $pathFormat = $config['scrawlPathFormat']; $maxSize = $config['scrawlMaxSize']; $allowFiles = $config['imageAllowFiles']; break; case 'uploadvideo': $fieldName = $config['videoFieldName']; $pathFormat = $config['videoPathFormat']; $maxSize = $config['videoMaxSize']; $allowFiles = $config['videoAllowFiles']; break; case 'uploadfile': $fieldName = $config['fileFieldName']; $pathFormat = $config['filePathFormat']; $maxSize = $config['fileMaxSize']; $allowFiles = $config['fileAllowFiles']; break; } if (isset($_FILES[$fieldName])) { $file = $_FILES[$fieldName]; // 检查文件大小 if ($file['size'] > $maxSize) { echo json_encode(array( 'state' => 'ERROR', 'message' => '文件大小超出限制' )); return; } // 检查文件类型 $fileExt = strtolower(strrchr($file['name'], '.')); if (!in_array($fileExt, $allowFiles)) { echo json_encode(array( 'state' => 'ERROR', 'message' => '不允许的文件类型' )); return; } // 生成文件路径 $savePath = generatePath($pathFormat); $saveDir = dirname($_SERVER['DOCUMENT_ROOT'] . $savePath); // 创建目录 if (!is_dir($saveDir)) { mkdir($saveDir, 0755, true); } $fullPath = $_SERVER['DOCUMENT_ROOT'] . $savePath; // 移动文件 if (move_uploaded_file($file['tmp_name'], $fullPath)) { echo json_encode(array( 'state' => 'SUCCESS', 'url' => $savePath, 'title' => $file['name'], 'original' => $file['name'], 'type' => $fileExt, 'size' => $file['size'] )); } else { echo json_encode(array( 'state' => 'ERROR', 'message' => '文件上传失败' )); } } else { echo json_encode(array( 'state' => 'ERROR', 'message' => '未找到上传文件' )); } } /** * 生成文件保存路径 */ function generatePath($pathFormat) { $path = $pathFormat; $path = str_replace('{yyyy}', date('Y'), $path); $path = str_replace('{mm}', date('m'), $path); $path = str_replace('{dd}', date('d'), $path); $path = str_replace('{time}', time(), $path); $path = preg_replace('/\{rand:(\d+)\}/', '', $path); $path .= '.' . substr(md5(uniqid()), 0, 6); return $path; } /** * 处理文件列表 */ function handleList($action, $config) { echo json_encode(array( 'state' => 'SUCCESS', 'list' => array(), 'start' => 0, 'total' => 0 )); } /** * 处理远程图片抓取 */ function handleCatchImage($config) { echo json_encode(array( 'state' => 'SUCCESS', 'list' => array() )); } ?>
Save Changes
Cancel