db689e72fcd883f9e81017a3ad278605eaea845f.svn-base 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * 获取已上传的文件列表
  4. * User: Jinqn
  5. * Date: 14-04-09
  6. * Time: 上午10:17
  7. */
  8. include "Uploader.class.php";
  9. /* 判断类型 */
  10. switch ($_GET['action']) {
  11. /* 列出文件 */
  12. case 'listfile':
  13. $allowFiles = $CONFIG['fileManagerAllowFiles'];
  14. $listSize = $CONFIG['fileManagerListSize'];
  15. $path = $CONFIG['fileManagerListPath'];
  16. break;
  17. /* 列出图片 */
  18. case 'listimage':
  19. default:
  20. $allowFiles = $CONFIG['imageManagerAllowFiles'];
  21. $listSize = $CONFIG['imageManagerListSize'];
  22. $path = $CONFIG['imageManagerListPath'];
  23. }
  24. $allowFiles = substr(str_replace(".", "|", join("", $allowFiles)), 1);
  25. /* 获取参数 */
  26. $size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $listSize;
  27. $start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0;
  28. $end = $start + $size;
  29. /* 获取文件列表 */
  30. $path = $_SERVER['DOCUMENT_ROOT'] . (substr($path, 0, 1) == "/" ? "":"/") . $path;
  31. $files = getfiles($path, $allowFiles);
  32. if (!count($files)) {
  33. return json_encode(array(
  34. "state" => "no match file",
  35. "list" => array(),
  36. "start" => $start,
  37. "total" => count($files)
  38. ));
  39. }
  40. /* 获取指定范围的列表 */
  41. $len = count($files);
  42. for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--){
  43. $list[] = $files[$i];
  44. }
  45. //倒序
  46. //for ($i = $end, $list = array(); $i < $len && $i < $end; $i++){
  47. // $list[] = $files[$i];
  48. //}
  49. /* 返回数据 */
  50. $result = json_encode(array(
  51. "state" => "SUCCESS",
  52. "list" => $list,
  53. "start" => $start,
  54. "total" => count($files)
  55. ));
  56. return $result;
  57. /**
  58. * 遍历获取目录下的指定类型的文件
  59. * @param $path
  60. * @param array $files
  61. * @return array
  62. */
  63. function getfiles($path, $allowFiles, &$files = array())
  64. {
  65. if (!is_dir($path)) return null;
  66. if(substr($path, strlen($path) - 1) != '/') $path .= '/';
  67. $handle = opendir($path);
  68. while (false !== ($file = readdir($handle))) {
  69. if ($file != '.' && $file != '..') {
  70. $path2 = $path . $file;
  71. if (is_dir($path2)) {
  72. getfiles($path2, $allowFiles, $files);
  73. } else {
  74. if (preg_match("/\.(".$allowFiles.")$/i", $file)) {
  75. $files[] = array(
  76. 'url'=> substr($path2, strlen($_SERVER['DOCUMENT_ROOT'])),
  77. 'mtime'=> filemtime($path2)
  78. );
  79. }
  80. }
  81. }
  82. }
  83. $files = list_sort_by($files, 'mtime', 'desc'); //按照时间排序
  84. return $files;
  85. }
  86. function list_sort_by($list, $field, $sortby='asc') {
  87. if(is_array($list)){ //判断是否数组
  88. $refer = $resultSet = array(); //初始化数组变量
  89. foreach ($list as $i => $data) //foreach数组
  90. $refer[$i] = &$data[$field]; //存储要排序的数组字段键和值
  91. switch ($sortby) {//进行排序
  92. case 'asc': // 正向排序
  93. asort($refer);
  94. break;
  95. case 'desc':// 逆向排序
  96. arsort($refer);
  97. break;
  98. case 'nat': // 自然排序
  99. natcasesort($refer);
  100. break;
  101. }
  102. foreach ( $refer as $key=> $val)//重新组合排序后的数组
  103. $resultSet[] = &$list[$key];
  104. return $resultSet;
  105. }
  106. return false;
  107. }