0) { $path = "Uploads/image/file/{$sellerid}/"; } break; /* 列出图片 */ case 'listimage': default: $allowFiles = $CONFIG['imageManagerAllowFiles']; $listSize = $CONFIG['imageManagerListSize']; $path = $CONFIG['imageManagerListPath']; $sellerid = isset($_GET['sellerid']) ? intval($_GET['sellerid']) : 0; if($sellerid > 0) { $path = "Uploads/image/goods_description/{$sellerid}/"; } } $allowFiles = substr(str_replace(".", "|", join("", $allowFiles)), 1); /* 获取参数 */ $size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $listSize; $start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0; $end = $start + $size; /* 获取文件列表 */ $path = $_SERVER['DOCUMENT_ROOT'] . (substr($path, 0, 1) == "/" ? "":"/") . $path; $files = getfiles($path, $allowFiles); if (!count($files)) { return json_encode(array( "state" => "no match file", "list" => array(), "start" => $start, "total" => count($files) )); } /* 获取指定范围的列表 */ $len = count($files); $list = $files; $list = my_sort($files,'mtime',SORT_DESC,SORT_NUMERIC); /** for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--){ $list[] = $files[$i]; } **/ /** //倒序 for ($i = $end, $list = array(); $i < $len && $i < $end; $i++){ $list[] = $files[$i]; } **/ /* 返回数据 */ $result = json_encode(array( "state" => "SUCCESS", "list" => $list, "start" => $start, "total" => count($files) )); return $result; function my_sort($arrays,$sort_key,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC ){ if(is_array($arrays)){ foreach ($arrays as $array){ if(is_array($array)){ $key_arrays[] = $array[$sort_key]; }else{ return false; } } }else{ return false; } array_multisort($key_arrays,$sort_order,$sort_type,$arrays); return $arrays; } /** * 遍历获取目录下的指定类型的文件 * @param $path * @param array $files * @return array */ function getfiles($path, $allowFiles, &$files = array()) { if (!is_dir($path)) return null; if(substr($path, strlen($path) - 1) != '/') $path .= '/'; $handle = opendir($path); while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') { $path2 = $path . $file; if (is_dir($path2)) { getfiles($path2, $allowFiles, $files); } else { if (preg_match("/\.(".$allowFiles.")$/i", $file)) { $files[] = array( 'url'=> substr($path2, strlen($_SERVER['DOCUMENT_ROOT'])), 'mtime'=> filemtime($path2) ); } } } } return $files; }