ueditor php上传文件注释
来源:原创
时间:2016-07-19
作者:脚本小站
分类:PHP
ueditor的php上传文件工作流程解释
controller.php
上传文件控制器,每个上传的文件都要经过此控制器
<?php //header('Access-Control-Allow-Origin: http://www.baidu.com'); //设置http://www.baidu.com允许跨域访问 //header('Access-Control-Allow-Headers: X-Requested-With,X_Requested_With'); //设置允许的跨域header date_default_timezone_set("Asia/chongqing"); error_reporting(E_ERROR); header("Content-Type: text/html; charset=utf-8"); $CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents("config.json")), true); $action = $_GET['action']; switch ($action){ case 'config': $result = json_encode($CONFIG); break; /* 上传文件操作:返回的数据格式 array( "state" => "", //上传状态,上传成功时必须返回"SUCCESS" "url" => "", //返回的地址 "title" => "", //新文件名 "original" => "", //原始文件名 "type" => "", //文件类型 "size" => "", //文件大小 ); */ /* 上传图片 */ case 'uploadimage': /* 上传涂鸦 */ case 'uploadscrawl': /* 上传视频 */ case 'uploadvideo': /* 上传文件 */ case 'uploadfile': $result = include("action_upload.php"); break; /* 列表操作:返回的数据格式 array( "state" => "SUCCESS", // 成功返回信息 "start" => $start, // 开始位置 "total" => count($files), // 文件个数统计 // 当前列出的文件列表 "list" => array( array('url' => '图片地址','mtime' => '时间戳'), array('url' => '图片地址','mtime' => '时间戳'), ); ) */ /* 列出图片 */ case 'listimage': $result = include("action_list.php"); break; /* 列出文件 */ case 'listfile': $result = include("action_list.php"); break; /* * 抓取远程文件操作:返回的数据格式 array( 'state' => count($list) ? 'SUCCESS':'ERROR', 'list' => array( array( "state" => $info["state"], "url" => $info["url"], "size" => $info["size"], "title" => htmlspecialchars($info["title"]), "original" => htmlspecialchars($info["original"]), "source" => htmlspecialchars($imgUrl) ) array( "state" => $info["state"], "url" => $info["url"], "size" => $info["size"], "title" => htmlspecialchars($info["title"]), "original" => htmlspecialchars($info["original"]), "source" => htmlspecialchars($imgUrl) ) ) ) */ /* 抓取远程文件 */ case 'catchimage': $result = include("action_crawler.php"); break; default: $result = json_encode(array( 'state'=> '请求地址出错' )); break; } /* 输出结果 */ if (isset($_GET["callback"])) { if (preg_match("/^[\w_]+$/", $_GET["callback"])) { echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')'; } else { echo json_encode(array('state'=> 'callback参数不合法')); } } else { echo $result; }
action_upload.php
上传文件操作,图片、视频、。。
<?php /** * 上传附件和上传视频 * User: Jinqn * Date: 14-04-09 * Time: 上午10:17 * 返回的数据格式 array( "state" => "", //上传状态,上传成功时必须返回"SUCCESS" "url" => "", //返回的地址 "title" => "", //新文件名 "original" => "", //原始文件名 "type" => "" //文件类型 "size" => "", //文件大小 ) */ include "Uploader.class.php"; /** * 上传配置 * 读取对应配置文件 * @param */ $base64 = "upload"; switch (htmlspecialchars($_GET['action'])) { case 'uploadimage': $config = array( "pathFormat" => $CONFIG['imagePathFormat'],// 上传图片命名规则 "maxSize" => $CONFIG['imageMaxSize'],// 上传图片文件最大大小 "allowFiles" => $CONFIG['imageAllowFiles']// 上传允许的图片类型 ); $fieldName = $CONFIG['imageFieldName'];//提交的图片表单名称 break; case 'uploadscrawl': $config = array( "pathFormat" => $CONFIG['scrawlPathFormat'],// 抓取文件的命名规则 "maxSize" => $CONFIG['scrawlMaxSize'],// 抓取文件最大大小 "allowFiles" => $CONFIG['scrawlAllowFiles'],// 允许抓取的文件类型 "oriName" => "scrawl.png" ); $fieldName = $CONFIG['scrawlFieldName'];//提交的图片表单名称 $base64 = "base64"; break; case 'uploadvideo': $config = array( "pathFormat" => $CONFIG['videoPathFormat'],// 视频文件命名规则 "maxSize" => $CONFIG['videoMaxSize'],// 视频文件最大大小 "allowFiles" => $CONFIG['videoAllowFiles']//允许的视频文件类型 ); $fieldName = $CONFIG['videoFieldName'];//提交的视频表单名称 break; case 'uploadfile': default: $config = array( "pathFormat" => $CONFIG['filePathFormat'],// 上传文件命名规则 "maxSize" => $CONFIG['fileMaxSize'],//上传文件的最大大小 "allowFiles" => $CONFIG['fileAllowFiles']// 允许的上传文件类型 ); $fieldName = $CONFIG['fileFieldName'];//提交的文件表单名称 break; } /* 生成上传实例对象并完成上传 */ /** * 上传文件类 * @param string $fileField 表单名称 * @param array $config 配置项 * @param bool $base64 是否解析base64编码,可省略。若开启,则$fileField代表的是base64编码的字符串表单名 */ $up = new Uploader($fieldName, $config, $base64); /** * 得到上传文件所对应的各个参数,数组结构 * array( "state" => "", //上传状态,上传成功时必须返回"SUCCESS" "url" => "", //返回的地址 "title" => "", //新文件名 "original" => "", //原始文件名 "type" => "" //文件类型 "size" => "", //文件大小 ) */ /* 返回数据 */ return json_encode($up->getFileInfo());
action_crawler.php
远程抓取文件
<?php /** * 抓取远程图片 * User: Jinqn * Date: 14-04-14 * Time: 下午19:18 * 返回的数据格式 array( 'state' => count($list) ? 'SUCCESS':'ERROR', 'list' => array( array( "state" => $info["state"], "url" => $info["url"], "size" => $info["size"], "title" => htmlspecialchars($info["title"]), "original" => htmlspecialchars($info["original"]), "source" => htmlspecialchars($imgUrl) ) array( "state" => $info["state"], "url" => $info["url"], "size" => $info["size"], "title" => htmlspecialchars($info["title"]), "original" => htmlspecialchars($info["original"]), "source" => htmlspecialchars($imgUrl) ) ) ) */ set_time_limit(0); include("Uploader.class.php"); /* 上传配置 */ $config = array( "pathFormat" => $CONFIG['catcherPathFormat'], "maxSize" => $CONFIG['catcherMaxSize'], "allowFiles" => $CONFIG['catcherAllowFiles'], "oriName" => "remote.png" ); $fieldName = $CONFIG['catcherFieldName']; /* 抓取远程图片 */ $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(); array_push( $list, array( "state" => $info["state"], "url" => $info["url"], "size" => $info["size"], "title" => htmlspecialchars($info["title"]), "original" => htmlspecialchars($info["original"]), "source" => htmlspecialchars($imgUrl) ) ); } /* 返回抓取数据 */ return json_encode( array( 'state' => count($list) ? 'SUCCESS':'ERROR', 'list' => $list ) );
action_list.php
获取文件列表
<?php /** * 获取已上传的文件列表 * User: Jinqn * Date: 14-04-09 * Time: 上午10:17 * * 要返回的数据的格式 array( "state" => "SUCCESS", // 成功返回信息 "start" => $start, // 开始位置 "total" => count($files), // 文件个数统计 // 当前列出的文件列表 "list" => array( array('url' => '图片地址','mtime' => '时间戳'), array('url' => '图片地址','mtime' => '时间戳'), ); ) */ include "Uploader.class.php"; /* 判断类型 */ switch ($_GET['action']) { /* 列出文件 */ case 'listfile': $allowFiles = $CONFIG['fileManagerAllowFiles']; $listSize = $CONFIG['fileManagerListSize']; $path = $CONFIG['fileManagerListPath']; break; /* 列出图片 */ case 'listimage': default: $allowFiles = $CONFIG['imageManagerAllowFiles']; $listSize = $CONFIG['imageManagerListSize']; $path = $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 = getfiles($path, $allowFiles); // 没有文件列表数据时返回 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]; //} /** * 返回数据文件列表 * @param */ $result = json_encode( array( "state" => "SUCCESS", // 成功返回信息 "list" => $list, // 当前列出的文件列表 "start" => $start, // 开始位置 "total" => count($files) // 文件个数统计 ) ); return $result; /** * 遍历获取目录下的指定类型的文件,并返回所有目录下文件的列表数组 * * @param string $path 文件路劲 * @param array $allowFiles 要列出列表的文件类型 * @param array $files [可选]要返回的数组,地址引用 * @return array array( array('url' => '图片地址','mtime' => '时间戳'), array('url' => '图片地址','mtime' => '时间戳'), ); */ function getfiles($path, $allowFiles, &$files = array()) { 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)) { getfiles($path2, $allowFiles, $files); } else { if (preg_match("/\.(".$allowFiles.")$/i", $file)) { $files[] = array( 'url' => substr($path2, strlen($_SERVER['DOCUMENT_ROOT'])), 'mtime' => filemtime($path2) ); } } } } return $files; }