引数で指定した起点ディレクトリ以下のファイル一覧を生成するスクリプト ver1.1

<?php

/**
 * 引数で指定した起点ディレクトリ以下のファイル一覧を生成するスクリプト
 * 
 * @version 1.1
 */


// 引数がディレクトリパスでなかったら、終了
if ( !is_dir($basePath = $argv[1]))
{
    echo "It is not a directory.";
}


// ファイルリストの配列
$fileList = array();
// ファイルリストを生成
makeFileList($basePath, $fileList);

// 確認
print_r($fileList);



/**
 * ディレクトリパスを受け取り、<br>
 * それ以下のファイル、及びディレクトリパスを生成する
 * 
 * @access public
 * @param  string $dirPath ディレクトリパス
 * @param  array  $fileList ファイルリスト
 */
function makeFileList($dirPath, &$fileList)
{
    // ディレクトリハンドラを開く
    //   有効なディレクトリでないか、
    //   または権限が制限されているか、
    //   ファイルシステムのエラーによりディレクトリがオープンできない場合はexit
    if ( !$dh = opendir($dirPath) )
    {
        exit;
    }

    // ファイルを一つずつ処理
    while (false !== ($entry = readdir($dh)))
    {
        // ファイルの絶対パスを生成
        $realPath = realpath($dirPath) . DIRECTORY_SEPARATOR . $entry;

        // "."と".."と自分のスクリプトファイル名は処理しない
        if ( "." === $entry || ".." === $entry || __FILE__ === $realPath)
        {
            continue;
        }

        // ファイルリストを追加
        $fileList[] = $realPath;

        // ディレクトリの場合は再帰処理
        if ( is_dir($realPath) )
        {
            makeFileList($realPath, $fileList);
        }
    }

    closedir($dh);

}

?>