2012-12-13 1 views
3

가능한 중복 :
Caching readdir()PHP : 각로드에있는 파일에 대한 디렉토리를 스캔

가 나는 내가 깨끗한 URL을 가질 수 있도록 내 사이트 설정 및 파일 자체가 다양한 수준에있다 의 폴더. 그것을 관리하기 위해 다음 스크립트를 사용하여 페이지에 더 멀리 포함시킵니다. 그것은 현재 모든 pageload에 사용하고, 나는 단지 내가 새 파일을 추가 할 때를 업데이트해야 될 때

function listFolderFiles($dir,$exclude){ 
    global $flist; 
    $ffs = scandir($dir); 
    foreach($ffs as $ff) 
    { 
     if(is_array($exclude) and !in_array($ff,$exclude)) 
     { 
      if($ff != '.' && $ff != '..') 
      { 
       if(!is_dir($dir.'/'.$ff)) 
       {  
      } 
      if(is_dir($dir.'/'.$ff)) 
       { 
       listFolderFiles($dir.'/'.$ff,$exclude); 
      } 
      if((is_dir($dir.'/'.$ff)) != 1 && strtolower(substr($ff, strrpos($ff, '.') + 1)) == 'php') 
       { 
        $name = basename($ff, ".php"); 
        $flist[] .= $name;     
       } 
      } 
     } 
    } 
} 
listFolderFiles('content/',array('filelist.php')); 
$removing = array('_en','_es'); 
$pages = str_replace($removing, "", $flist); 
unset($dir, $exclude, $flist); 
listFolderFiles('content/Beginner/',array('filelist.php')); 
$removing = array('_en','_es'); 
$beginner = str_replace($removing, "", $flist); 
unset($dir, $exclude, $flist); 
listFolderFiles('content/Intermediate/',array('filelist.php', $beginner)); 
$removingi = array('_en','_es'); 
$intermediate = str_replace($removingi, "", $flist); 
unset($dir, $exclude, $flist); 
listFolderFiles('content/Advanced/',array('filelist.php', $beginner)); 
$removinga = array('_en','_es'); 
$advanced = str_replace($removinga, "", $flist); 

는이 코드를 실행하는 빠른 방법이있다.

+0

사이드 노트 :'global $ flist; '의 용도는 무엇입니까? 왜 함수에서 $ flist를 반환하지 않는 것이 좋을까요? –

+3

필요한 파일 만 포함하는 것이 어떻습니까? – Ibu

+0

디렉토리 반복자를 보았습니까? http://php.net/manual/en/directoryiterator.construct.php – Drewdin

답변

1

알고리즘 개선 외에도 캐싱을위한 훌륭한 후보입니다. 배열을 직렬화하고 파일에 저장하는 것처럼 간단 할 수 있습니다. 그런 다음 만료 정책이 무엇인지 결정할 수 있습니다. 의사 코드 :

if(!file_exists(CACHE_NAME) || dir_cache_expired(CACHE_NAME)) { 
    // ... code to build $dirlist 
    file_put_contents(CACHE_NAME, serialize($dirlist)); 
} else { 
    $dirlist = unserialize(file_get_contents(CACHE_NAME)); 
} 

이렇게하면 캐시 파일을 간단히 삭제하여 캐시를 다시 시작할 수 있습니다.

+0

그게 내가 찾고 있었던거야! 감사합니다 ctrahey! –

관련 문제