比如說我用代碼只能列出目前的目錄的檔案及目錄。
bak
1.php
1.txt
123
........
但是我發現了 我是要bak檔案夾下的檔案。(bak目錄的絕對路徑和相對路徑都是已知的。)
請賜代碼 本人不太懂得寫代碼.
回複討論(解決方案)
你寫的不對,當然就不行
function get_files($dir){$files = array();if(is_dir($dir)){if($dh = opendir($dir)){while (($file = readdir($dh)) !== false) {if(!($file == '.' || $file == '..')){$file = $dir.'/'.$file;if(is_dir($file) && $file != './.' && $file != './..'){$files = array_merge($files, get_files($file));}else if(is_file($file)){$fullpath = $_SERVER['HTTP_REFERER'];$fullpath = str_replace(basename($fullpath),"",$fullpath);$fullpath .= substr($file,2);$files[] = $fullpath;}}}}}return $files;}
使用方法
$path = '../bak';
foreach(get_files($path) as $allfile){
echo $allfile.'';
}
使用迭代器,既簡單又迅速
$path = '目錄名';$ite = new RecursiveDirectoryIterator($path);foreach (new RecursiveIteratorIterator($ite) as $filename=>$cur) { $fn = $cur->getBasename(); if($cur->isDir() || ($fn == '.' || $fn == '..')) continue; echo $filename, '
';}
既然用迭代器,就不用判斷isdir那麼麻煩,預設就是leaves only的
$items = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));foreach($items as $itemName => $item) echo (string)$itemName, '
', PHP_EOL;
使用迭代器,既簡單又迅速
$path = '目錄名';$ite = new RecursiveDirectoryIterator($path);foreach (new RecursiveIteratorIterator($ite) as $filename=>$cur) { $fn = $cur->getBasename(); if($cur->isDir() || ($fn == '.' || $fn == '..')) continue; echo $filename, '
';}
csdn總算能看到引用的代碼了……呵呵
我是來學習的
既然用迭代器,就不用判斷isdir那麼麻煩,預設就是leaves only的
$items = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));foreach($items as $itemName => $item) echo (string)$itemName, '
', PHP_EOL;
使用迭代器,既簡單又迅速
$path = '目錄名';$ite = new RecursiveDirectoryIterator($path);foreach (new RecursiveIteratorIterator($ite) as $filename=>$cur) { $fn = $cur->getBasename(); if($cur->isDir() || ($fn == '.' || $fn == '..')) continue; echo $filename, '
';} 太犀利了!
是嗎?
像 app/Admin/.. 這樣的不是也輸出了嗎?
倒是只判斷 isDir() 就可以只輸出檔案名了
既然用迭代器,就不用判斷isdir那麼麻煩,預設就是leaves only的
$items = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));foreach($items as $itemName => $item) echo (string)$itemName, '
', PHP_EOL;
使用迭代器,既簡單又迅速
$path = '目錄名';$ite = new RecursiveDirectoryIterator($path);foreach (new RecursiveIteratorIterator($ite) as $filename=>$cur) { $fn = $cur->getBasename(); if($cur->isDir() || ($fn == '.' || $fn == '..')) continue; echo $filename, '
';}
沒有一個可以 繼續等完整代碼中。。。。。。
php文檔內說的遞迴器就是預設去枝留葉方式,要輸出節點應轉換參數,如果有輸出dir名那就不清楚是否未完善的bug
是嗎?
像 app/Admin/.. 這樣的不是也輸出了嗎?
倒是只判斷 isDir() 就可以只輸出檔案名了
說一些有用的東西吧~!繼續等完整代碼中。。。。。。
真奇了怪了!
你說說上面那段代碼沒有用?
超出想象,難道不懂怎麼賦值給$path還是不懂從返回中提取?