[PHP] サブディレクトリ内のファイル名をすべて取得

function get_files($dir, $pre = '', $excluded = '') {
    $result = array();
    $excluded = empty($excluded)
        ? array('.'.DIRECTORY_SEPARATOR, '..'.DIRECTORY_SEPARATOR)
        : (array)$excluded;
    if ( is_dir($dir) ) {
        if ( $dh = opendir($dir) ) {
            while ( ($file = readdir($dh)) !== FALSE ) {
                if ( is_dir($dir.$file) ) {
                    $file .= DIRECTORY_SEPARATOR;
                    if ( !(in_array($file, $excluded) || in_array($pre.$file, $excluded)) ) {
                        $result = array_merge($result, get_files($dir.$file, $pre.$file, $excluded));
                    }
                } else if ( !(in_array($file, $excluded) || in_array($pre.$file, $excluded)) ) {
                    $result[] = $pre.$file;
                }
            }
            closedir($dh);
        }
    }
    return $result;
}
$files = get_files('/path/to/');

Posted

in

by

Tags:

Comments

コメントを残す

以下に詳細を記入するか、アイコンをクリックしてログインしてください。

WordPress.com ロゴ

WordPress.com アカウントを使ってコメントしています。 ログアウト /  変更 )

Facebook の写真

Facebook アカウントを使ってコメントしています。 ログアウト /  変更 )

%s と連携中

%d人のブロガーが「いいね」をつけました。