PHPエルゴードフォルダとサブディレクトリの関数コードを使用します。


私たちが使用する関数はScandirです。その役割は指定されたパスのファイルとディレクトリをリストすることです。Dirのように。
>より強力なGlob()関数とは、指定されたモードにマッチするファイル名またはディレクトリを配列形式で返す役割を果たしている。友情は注意して、くれぐれも小さい正常でないようにコンピュータの前でぼうっとしているのがあまりに長い時間が要らないで、さもなくば小さい正常ではありませんように幽霊の高血糖に見えます。一.ワンフロアフォルダを巡回した:>>単層フォルダをスキャンする問題は、二つの関数の結果は違っていますが、表現は大きく違っています。Scandir関数は追加の2行を提供します。それぞれ「.」と「.」です。Globはありません。
 
function get_dir_scandir(){
$tree = array();
foreach(scandir('./') as $single){
echo $single."<br/>\r
";
}
}
get_dir_scandir();

function get_dir_glob(){
$tree = array();
foreach(glob('./*') as $single){
echo $single."<br/>\r
";
}
}
get_dir_glob();
二.再帰的にファイルツリーを巡回しました:>>再帰的にフォルダツリーをスキャンする問題で、やはりGlob関数の表現がいいです。正確に言います。Scrandir関数は2回のスキャンを意味不明にします。b.phpと./a.phpはスキャンレポートの上に二回現れます。とても不思議です。
 
//Update at 2010.07.25 -
$path = '..';
function get_filetree_scandir($path){
$tree = array();
foreach(scandir($path) as $single){
if(is_dir('../'.$single)){
$tree = array_merge($tree,get_filetree($single));
}
else{
$tree[] = '../'.$single;
}
}
return $tree;
}
print_r(get_filetree_scandir($path));

//Update at 2010.07.25 -
$path = './';
function get_filetree_scandir($path){
$result = array();
$temp = array();
if (!is_dir($path)||!is_readable($path)) return null; //
$allfiles = scandir($path); //
foreach ($allfiles as $filename) { //
if (in_array($filename,array('.','..'))) continue; // . ..
$fullname = $path.'/'.$filename; //
if (is_dir($fullname)) { //
$result[$filename] = get_filetree_scandir($fullname); //
}
else {
$temp[] = $filename; // ,
}
}
foreach ($temp as $tmp) { //
$result[] = $tmp; // ,
}
return $result;
}
print_r(get_filetree_scandir($path));
>Glob関数はグレーをスキャンするのがとても正確で、しかも自動的に字母によって順番を並べて、最も良い方案のようです。
 
$path = '..';
function get_filetree($path){
$tree = array();
foreach(glob($path.'/*') as $single){
if(is_dir($single)){
$tree = array_merge($tree,get_filetree($single));
}
else{
$tree[] = $single;
}
}
return $tree;
}
print_r(get_filetree($path));