PHP 擷取指定目錄下所有檔案(包含子目錄),_PHP教程

來源:互聯網
上載者:User

PHP 擷取指定目錄下所有檔案(包含子目錄),


PHP 擷取指定目錄下所有檔案(包含子目錄)

測試環境在linux下,如果要在windows,請改正$new_dir中的目錄路徑尋找方式

php function get_file_list($dir){      $file_list = array();    $file_dir_list = array();         $dir_list = scandir($dir); //尋找目錄           foreach ($dir_list as $r)    {        if ($r == '.' || $r == '..')          {            continue;        }        $new_dir = $dir . '/' . $r;        if (is_dir($new_dir))        {            $file_dir = get_file_list($new_dir);            $file_dir_list = array_merge($file_dir_list, $file_dir);        }        else        {            $file_list[] = $new_dir;        }    }         return array_merge($file_list, $file_dir_list);} $file_list = get_file_list('.');print_r($file_list);

scandir定義和用法

scandir() 函數返回一個數組,其中包含指定路徑中的檔案和目錄。

若成功,則返回一個數組,若失敗,則返回 false。如果 directory 不是目錄,則返回布爾值 false 並產生一條 E_WARNING 級的錯誤。

文法

scandir(directory,sort,context)
參數 描述
directory 必需。規定要掃描的目錄。
sort 可選。規定排列順序。預設是 0 (升序)。如果是 1,則為降序。
context 可選。規定目錄控制代碼的環境。context 是可修改目錄流的行為的一套選項。

例子

phpprint_r(scandir("images"));?> 

輸出:

Array([0] => .[1] => ..[2] => dog.jpg[3] => house.jpg[4] => logo.gif)

http://www.bkjia.com/PHPjc/1106269.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1106269.htmlTechArticlePHP 擷取指定目錄下所有檔案(包含子目錄), PHP 擷取指定目錄下所有檔案(包含子目錄) 測試環境在linux下,如果要在windows,請改正$n...

  • 聯繫我們

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