파일 이름 정렬

파일 이름 정렬

QA

파일 이름 정렬

본문

어디다 sort 먹여야 재대로 정렬이 될 까요? 고수님들 도와 주세욬1

 


<?php
function searchDir($base_dir="./",$p="",$f="",$allowed_depth=-1){
    $contents=array();
    $base_dir=trim($base_dir);
    $p=trim($p);
    $f=trim($f);
    if($base_dir=="")$base_dir="./";
    if(substr($base_dir,-1)!="/")$base_dir.="/";
    $p=str_replace(array("../","./"),"",trim($p,"./"));
    $p=$base_dir.$p;
    
    if(!is_dir($p))$p=dirname($p);
    if(substr($p,-1)!="/")$p.="/";
    if($allowed_depth>-1){
        $allowed_depth=count(explode("/",$base_dir))+ $allowed_depth-1;
        $p=implode("/",array_slice(explode("/",$p),0,$allowed_depth));
        if(substr($p,-1)!="/")$p.="/";
    }
    $filter=($f=="")?array():explode(",",strtolower($f));
    $files=@scandir($p);
    
    if(!$files)return array("contents"=>array(),"currentPath"=>$p);
    
    for ($i=0;$i<count($files);$i++){
        $fName=$files[$i];
        $fPath=$p.$fName;
        $isDir=is_dir($fPath);
        $add=false;
        $fType="folder";
        
        if(!$isDir){
            $ft=strtolower(substr($files[$i],strrpos($files[$i],".")+1));
            $fType=$ft;    
            if($f!=""){
                if(in_array($ft,$filter))$add=true;
            }else{
                $add=true;
            }
        }else{
            if($fName==".")continue;
            $add=true;
            
            if($f!=""){
                if(!in_array($fType,$filter))$add=false;
            }
            if($fName==".."){
                if($p==$base_dir){
                    $add=false;
                }else $add=true;
                
                $tempar=explode("/",$fPath);
                array_splice($tempar,-2);
                $fPath=implode("/",$tempar);
                if(strlen($fPath)<= strlen($base_dir))$fPath="";
            }
        }
        if($fPath!="")$fPath=substr($fPath,strlen($base_dir));
        if($add) $contents[]=array("fPath"=>$fPath,"fName"=>$fName,"fType"=>$fType);
    }
    $p=(strlen($p)<= strlen($base_dir))?$p="":substr($p,strlen($base_dir));
    return array("contents"=>$contents,"currentPath"=>$p);
}
$p=isset($_POST["path"])?$_POST["path"]:"";
$f=isset($_POST["filter"])?$_POST["filter"]:"";
echo json_encode(searchDir("C:/Anime",$p,$f,-1));
?>

이 질문에 댓글 쓰기 :

답변 1

for 위에다가 보여줄  sort 먹이시면 될꺼같아요 

$filter=($f=="")?array():explode(",",strtolower($f));
    $files=@scandir($p);

    sort($files); // 정렬

    if(!$files)return array("contents"=>array(),"currentPath"=>$p);
   
    for ($i=0;$i<count($files);$i++){

이렇게 하란 말씀이신가요?

답변을 작성하시기 전에 로그인 해주세요.
전체 14
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1402호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT