列出指定目錄下所有檔案的PHP代碼?

來源:互聯網
上載者:User
比如說我用代碼只能列出目前的目錄的檔案及目錄。
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還是不懂從返回中提取?

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.