關於 PHP, Linux, Open Source 及個人生活記載的網誌。
RSS icon
  • PHP 列出目錄內容

    Posted on August 26th, 2007 Sam Tang No comments

    以下是用 PHP 列出目錄內容的方法,當然也可以略為修改以程式碼改為 function 使用:

    PHP:
    1. <?php
    2. // 定義要開啟的目錄
    3. $dir = "/var/www/vhosts/dir";
    4.  
    5. // 用 opendir() 開啟目錄,開啟失敗終止程式
    6. $handle = @opendir($dir) or die("Cannot open " . $dir);
    7.  
    8. echo "<b>Files in " . $dir . ":</b><br/>";
    9.  
    10. // 用 readdir 讀取檔案內容
    11. while($file = readdir($handle)){
    12.     // 將 "." 及 ".." 排除不顯示
    13.     if($file != "." && $file != ".."){
    14.         echo "$file<br/>";
    15.     }
    16. }
    17.  
    18. // 關閉目錄
    19. closedir($handle);
    20. ?>


    Leave a reply

    *
    To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
    Click to hear an audio file of the anti-spam word