action_list.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. return $files;
  84. }