action_list.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // require_once '../../../../runtime.php';
  33. // require_once '../../../../func.php';
  34. // $files = M('Image')->select()
  35. // ->order('id DESC')
  36. // ->paginator(20, $size)
  37. // ->fetchRows()
  38. // ->toArray();
  39. foreach($files as $row) {
  40. $list[] = array(
  41. 'url' => $row['src'],
  42. 'mtime' => $row['create_time']
  43. );
  44. }
  45. if (!count($files)) {
  46. return json_encode(array(
  47. "state" => "no match file",
  48. "list" => array(),
  49. "start" => $start,
  50. "total" => count($files)
  51. ));
  52. }
  53. /* 获取指定范围的列表 */
  54. $len = count($files);
  55. for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--){
  56. $list[] = $files[$i];
  57. }
  58. //倒序
  59. //for ($i = $end, $list = array(); $i < $len && $i < $end; $i++){
  60. // $list[] = $files[$i];
  61. //}
  62. /* 返回数据 */
  63. $result = json_encode(array(
  64. "state" => "SUCCESS",
  65. "list" => $list,
  66. "start" => $start,
  67. "total" => count($files)
  68. ));
  69. return $result;
  70. /**
  71. * 遍历获取目录下的指定类型的文件
  72. * @param $path
  73. * @param array $files
  74. * @return array
  75. */
  76. function getfiles($path, $allowFiles, &$files = array())
  77. {
  78. if (!is_dir($path)) return null;
  79. if(substr($path, strlen($path) - 1) != '/') $path .= '/';
  80. $handle = opendir($path);
  81. while (false !== ($file = readdir($handle))) {
  82. if ($file != '.' && $file != '..') {
  83. $path2 = $path . $file;
  84. if (is_dir($path2)) {
  85. getfiles($path2, $allowFiles, $files);
  86. } else {
  87. if (preg_match("/\.(".$allowFiles.")$/i", $file)
  88. && !preg_match("/_(\d+)x(\d+)/i", $file) //过滤缩略图
  89. ) {
  90. $files[] = array(
  91. 'url'=> substr($path2, strlen($_SERVER['DOCUMENT_ROOT'])),
  92. 'mtime'=> filemtime($path2)
  93. );
  94. }
  95. }
  96. }
  97. }
  98. return $files;
  99. }