'2018/11/09'에 해당되는 글 1건

728x90

Audio Streaming 서버 코드를 구현한 걸 오픈한다.


<?php
$search = isset($_POST['audioPath'])? str_replace("/mp3files","",$_POST['audioPath']) : '';
$dir ='./mp3files'.$search;

echo getCurrentFileList($dir);
function getCurrentFileList($dir){
    $web_url = "http://localhost/_test";
    $valid_formats = array("mp3","wav");
    $handle = opendir($dir); // 디렉토리의 핸들을 얻어옴
    // 지정한 디렉토리에 있는 디렉토리와 파일들의 이름을 배열로 읽어들임
    $R = array(); // 결과 담을 변수 생성
    $cnt = 0;
    while ($entry = readdir($handle)) {
        if($entry == '.' || $entry == '..') continue;
        $getExt = pathinfo($entry, PATHINFO_EXTENSION); // 파일 확장자 구하기
        $curdir = substr($dir,1,strlen($dir)-1); // 현재 폴더 정보
        if(empty($getExt)){
            $subdir = $dir.'/'.$entry;
            $subdir = substr($subdir,1,strlen($subdir)-1);
            array_push($R,array("isFile"=>1,"fileName"=>$entry,"filePath"=>$subdir,"curPath"=>$curdir));
        } else {
            if(in_array($getExt, $valid_formats)){
                $filepath = $dir.'/'.$entry;
                $filepath = substr($filepath,1,strlen($filepath)-1);
                array_push($R,array("isFile"=>2,"fileName"=>$entry,"filePath"=>$filepath,"curPath"=>""));
            }
        }

        // 상위폴더
        $check = str_replace("/mp3files","",$curdir);
        if(strlen($check)>0 && $cnt < 1){ // 상위폴더는 1회만 추가되도록 처리
            $filePath = substr($curdir,0,strripos($curdir, "/")); // 상위폴더
            $updir = substr($filePath,0,strripos($filePath, "/")); // 차상위폴더
            array_push($R,array("isFile"=>0,"fileName"=>"상위폴더","filePath"=>$filePath,"curPath"=>$updir));
            $cnt++;
        }

    }
    closedir($handle);
    sort($R); // 안드로이드에서 가나다순으로 정렬하기 위해
    return json_encode(array("result"=>$R));
}

?>
 


블로그 이미지

Link2Me

,