파일 이름 정렬 채택완료

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

 

Copy
<?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개

채택된 답변
+20 포인트

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

로그인 후 평가할 수 있습니다

답변에 대한 댓글 4개

$AAA = json_encode(searchDir("C:/Anime",$p,$f,-1));
sort($AAA);
echo $AAA;

으로 하면 되나요? 해봤는데 안되는거 같아서요
$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++){

이렇게 하란 말씀이신가요?
for 위에서 정렬하고

for문에서 찍어보면 정렬이 되있어야되는데
정확한걸 원하시면
https://blog.naver.com/PostView.nhn?isHttpsRedirect=true&blogId=coding-&logNo=221412525462&redirect=Dlog&widgetTypeCall=true&directAccess=false
이거 참고하셔요

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인
🐛 버그신고