復制代碼 代碼如下:
function get_dir_scandir(){
$tree = array();
foreach(scandir('./') as $single){
echo $single."<br/>/r/n";
}
}
get_dir_scandir();
function get_dir_glob(){
$tree = array();
foreach(glob('./*') as $single){
echo $single."<br/>/r/n";
}
}
get_dir_glob();
復制代碼 代碼如下:
//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));
復制代碼 代碼如下:
$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));
新聞熱點
疑難解答