123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- /**
- * 获取已上传的文件列表
- * User: Jinqn
- * Date: 14-04-09
- * Time: 上午10:17
- */
- 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];
- //}
- /* 返回数据 */
- $result = json_encode(array(
- "state" => "SUCCESS",
- "list" => $list,
- "start" => $start,
- "total" => count($files)
- ));
- return $result;
- /**
- * 遍历获取目录下的指定类型的文件
- * @param $path
- * @param array $files
- * @return array
- */
- 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)
- );
- }
- }
- }
- }
- $files = list_sort_by($files, 'mtime', 'desc'); //按照时间排序
- return $files;
- }
- function list_sort_by($list, $field, $sortby='asc') {
- if(is_array($list)){ //判断是否数组
- $refer = $resultSet = array(); //初始化数组变量
- foreach ($list as $i => $data) //foreach数组
- $refer[$i] = &$data[$field]; //存储要排序的数组字段键和值
- switch ($sortby) {//进行排序
- case 'asc': // 正向排序
- asort($refer);
- break;
- case 'desc':// 逆向排序
- arsort($refer);
- break;
- case 'nat': // 自然排序
- natcasesort($refer);
- break;
- }
- foreach ( $refer as $key=> $val)//重新组合排序后的数组
- $resultSet[] = &$list[$key];
- return $resultSet;
- }
- return false;
- }
|