file_manager_json.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * KindEditor PHP
  4. *
  5. * 本PHP程序是演示程序,建议不要直接在实际项目中使用。
  6. * 如果您确定直接使用本程序,使用之前请仔细确认相关安全设置。
  7. *
  8. */
  9. ini_set('date.timezone','Asia/Shanghai');
  10. require '../check.php';
  11. require_once 'JSON.php';
  12. $php_path = dirname(__FILE__) . '/';
  13. $php_url = dirname($_SERVER['PHP_SELF']);
  14. //根目录路径,可以指定绝对路径,比如 /var/www/attached/
  15. $root_path = $php_path . '/../../../../public/attachment/';
  16. //根目录URL,可以指定绝对路径,比如 http://www.yoursite.com/attached/
  17. $root_url = $php_url . '/../../../../public/attachment/';
  18. //图片扩展名
  19. $cfg_ext = strim(app_conf("ALLOW_IMAGE_EXT"));
  20. $arr = explode(",", $cfg_ext);
  21. if(count($arr)>0)
  22. $ext_arr = $arr;
  23. else
  24. $ext_arr = array('jpg', 'jpeg', 'png','gif');
  25. //目录名
  26. $dir_name = empty($_GET['dir']) ? '' : trim($_GET['dir']);
  27. if (!in_array($dir_name, array('', 'image', 'flash', 'media', 'file'))) {
  28. echo "Invalid Directory name.";
  29. exit;
  30. }
  31. /*if ($dir_name !== '') {
  32. $root_path .= $dir_name . "/";
  33. $root_url .= $dir_name . "/";
  34. if (!file_exists($root_path)) {
  35. mkdir($root_path);
  36. }
  37. }*/
  38. //根据path参数,设置各路径和URL
  39. if (empty($_GET['path'])) {
  40. $current_path = realpath($root_path) . '/';
  41. $current_url = $root_url;
  42. $current_dir_path = '';
  43. $moveup_dir_path = '';
  44. } else {
  45. $current_path = realpath($root_path) . '/' . $_GET['path'];
  46. $current_url = $root_url . $_GET['path'];
  47. $current_dir_path = $_GET['path'];
  48. $moveup_dir_path = preg_replace('/(.*?)[^\/]+\/$/', '$1', $current_dir_path);
  49. }
  50. //echo realpath($root_path);
  51. //排序形式,name or size or type
  52. $order = empty($_GET['order']) ? 'name' : strtolower($_GET['order']);
  53. //不允许使用..移动到上一级目录
  54. if (preg_match('/\.\./', $current_path)) {
  55. echo 'Access is not allowed.';
  56. exit;
  57. }
  58. //最后一个字符不是/
  59. if (!preg_match('/\/$/', $current_path)) {
  60. echo 'Parameter is not valid.';
  61. exit;
  62. }
  63. //目录不存在或不是目录
  64. if (!file_exists($current_path) || !is_dir($current_path)) {
  65. echo 'Directory does not exist.';
  66. exit;
  67. }
  68. //遍历目录取得文件信息
  69. $file_list = array();
  70. if ($handle = opendir($current_path)) {
  71. $i = 0;
  72. while (false !== ($filename = readdir($handle))) {
  73. if ($filename{0} == '.') continue;
  74. $file = $current_path . $filename;
  75. if (is_dir($file)) {
  76. $file_list[$i]['is_dir'] = true; //是否文件夹
  77. $file_list[$i]['has_file'] = (count(scandir($file)) > 2); //文件夹是否包含文件
  78. $file_list[$i]['filesize'] = 0; //文件大小
  79. $file_list[$i]['is_photo'] = false; //是否图片
  80. $file_list[$i]['filetype'] = ''; //文件类别,用扩展名判断
  81. } else {
  82. preg_match("/_[\d]+[x][\d]+/i", $file,$match_res);
  83. if(count($match_res)>0)continue;
  84. $file_list[$i]['is_dir'] = false;
  85. $file_list[$i]['has_file'] = false;
  86. $file_list[$i]['filesize'] = filesize($file);
  87. $file_list[$i]['dir_path'] = '';
  88. $file_ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
  89. $file_list[$i]['is_photo'] = in_array($file_ext, $ext_arr);
  90. $file_list[$i]['filetype'] = $file_ext;
  91. }
  92. $file_list[$i]['filename'] = $filename; //文件名,包含扩展名
  93. $file_list[$i]['datetime'] = date('Y-m-d H:i:s', filemtime($file)); //文件最后修改时间
  94. $i++;
  95. }
  96. closedir($handle);
  97. }
  98. //排序
  99. function cmp_func($a, $b) {
  100. global $order;
  101. if ($a['is_dir'] && !$b['is_dir']) {
  102. return -1;
  103. } else if (!$a['is_dir'] && $b['is_dir']) {
  104. return 1;
  105. } else {
  106. if ($order == 'size') {
  107. if ($a['filesize'] > $b['filesize']) {
  108. return 1;
  109. } else if ($a['filesize'] < $b['filesize']) {
  110. return -1;
  111. } else {
  112. return 0;
  113. }
  114. } else if ($order == 'type') {
  115. return strcmp($a['filetype'], $b['filetype']);
  116. } else {
  117. return strcmp($a['filename'], $b['filename']);
  118. }
  119. }
  120. }
  121. usort($file_list, 'cmp_func');
  122. $result = array();
  123. //相对于根目录的上一级目录
  124. $result['moveup_dir_path'] = $moveup_dir_path;
  125. //相对于根目录的当前目录
  126. $result['current_dir_path'] = $current_dir_path;
  127. //当前目录的URL
  128. $result['current_url'] = $current_url;
  129. //文件数
  130. $result['total_count'] = count($file_list);
  131. //文件列表数组
  132. $result['file_list'] = $file_list;
  133. //输出JSON字符串
  134. header('Content-type: application/json; charset=UTF-8');
  135. $json = new Services_JSON();
  136. echo $json->encode($result);