action_list.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. $sellerid = isset($_GET['sellerid']) ? intval($_GET['sellerid']) : 0;
  17. if($sellerid > 0)
  18. {
  19. $path = "Uploads/image/file/{$sellerid}/";
  20. }
  21. break;
  22. /* 列出图片 */
  23. case 'listimage':
  24. default:
  25. $allowFiles = $CONFIG['imageManagerAllowFiles'];
  26. $listSize = $CONFIG['imageManagerListSize'];
  27. $path = $CONFIG['imageManagerListPath'];
  28. $sellerid = isset($_GET['sellerid']) ? intval($_GET['sellerid']) : 0;
  29. if($sellerid > 0)
  30. {
  31. $path = "Uploads/image/goods_description/{$sellerid}/";
  32. }
  33. }
  34. $allowFiles = substr(str_replace(".", "|", join("", $allowFiles)), 1);
  35. /* 获取参数 */
  36. $size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $listSize;
  37. $start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0;
  38. $end = $start + $size;
  39. /* 获取文件列表 */
  40. $path = $_SERVER['DOCUMENT_ROOT'] . (substr($path, 0, 1) == "/" ? "":"/") . $path;
  41. $files = getfiles($path, $allowFiles);
  42. if (!count($files)) {
  43. return json_encode(array(
  44. "state" => "no match file",
  45. "list" => array(),
  46. "start" => $start,
  47. "total" => count($files)
  48. ));
  49. }
  50. /* 获取指定范围的列表 */
  51. $len = count($files);
  52. $list = $files;
  53. $list = my_sort($files,'mtime',SORT_DESC,SORT_NUMERIC);
  54. /**
  55. for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--){
  56. $list[] = $files[$i];
  57. }
  58. **/
  59. /**
  60. //倒序
  61. for ($i = $end, $list = array(); $i < $len && $i < $end; $i++){
  62. $list[] = $files[$i];
  63. }
  64. **/
  65. /* 返回数据 */
  66. $result = json_encode(array(
  67. "state" => "SUCCESS",
  68. "list" => $list,
  69. "start" => $start,
  70. "total" => count($files)
  71. ));
  72. return $result;
  73. function my_sort($arrays,$sort_key,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC ){
  74. if(is_array($arrays)){
  75. foreach ($arrays as $array){
  76. if(is_array($array)){
  77. $key_arrays[] = $array[$sort_key];
  78. }else{
  79. return false;
  80. }
  81. }
  82. }else{
  83. return false;
  84. }
  85. array_multisort($key_arrays,$sort_order,$sort_type,$arrays);
  86. return $arrays;
  87. }
  88. /**
  89. * 遍历获取目录下的指定类型的文件
  90. * @param $path
  91. * @param array $files
  92. * @return array
  93. */
  94. function getfiles($path, $allowFiles, &$files = array())
  95. {
  96. if (!is_dir($path)) return null;
  97. if(substr($path, strlen($path) - 1) != '/') $path .= '/';
  98. $handle = opendir($path);
  99. while (false !== ($file = readdir($handle))) {
  100. if ($file != '.' && $file != '..') {
  101. $path2 = $path . $file;
  102. if (is_dir($path2)) {
  103. getfiles($path2, $allowFiles, $files);
  104. } else {
  105. if (preg_match("/\.(".$allowFiles.")$/i", $file)) {
  106. $files[] = array(
  107. 'url'=> substr($path2, strlen($_SERVER['DOCUMENT_ROOT'])),
  108. 'mtime'=> filemtime($path2)
  109. );
  110. }
  111. }
  112. }
  113. }
  114. return $files;
  115. }