php is_file()和is_dir()用於遍曆目錄時用法注意事項_PHP教程

來源:互聯網
上載者:User
1、目錄inc有以下內容:
子目錄 0
子目錄 a
footer.html
header.html
login_function.inc.php
mysqli_connect.php
style.css

2、現在PHP要遍曆inc目錄,並只顯示檔案,不顯示目錄0和a,代碼如下:
複製代碼 代碼如下:
$dir = $_SERVER['DOCUMENT_ROOT'];
$dir = "$dir/inc/";
$d = opendir($dir);
while(false !==($f=readdir($d)))
{
if(is_file($f)){
echo "

$f

";
}else{
echo "

是目錄$f

";
}
}
closedir($d);

結果卻只顯示了“footer.html”是檔案,其它都變成目錄了:
是目錄.
是目錄..
是目錄a
footer.html
是目錄header.html
是目錄login_function.inc.php
是目錄mysqli_connect.php
是目錄style.css

這是由於不能在is_file和is_dir中直接使用“$f”,這樣會被PHP當作是根目錄下的該檔案,而在我的根目錄下有footer.html這個檔案,所以會正確顯示這個檔案。其它則不行。代碼改成:
要正確顯示,需要改造代碼:
複製代碼 代碼如下:
while(false !== ($f=readdir($d)))
{
if(is_file("$dir/$f")){
echo "

$f

";
}else{
echo "

是目錄$f

";
}
}
closedir($d);

http://www.bkjia.com/PHPjc/321411.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/321411.htmlTechArticle1、目錄inc有以下內容: 子目錄 0 子目錄 a footer.html header.html login_function.inc.php mysqli_connect.php style.css 2、現在PHP要遍曆inc目錄,並只顯示檔案...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.