指定した起点ディレクトリ以下のファイル一覧を表示するスクリプト

<?php
/**
 * 指定した起点ディレクトリ以下のファイル一覧を表示するスクリプト
 * 
 */

// 引数を取得
$basepath = $argv[1];

// ファイルリストを取得
$fileList = array();
$fileList = getFileList($basepath, $fileList);

// 表示
print_r($fileList);



/**
 * ファイルリストを再帰的に取得する
 * 
 * @access public
 * @param  string $path ファイルパス(file or dir)
 * @param  array  $buf ファイルリストを溜め込む箱
 */
function getFileList($path, &$buf)
{
    // ディレクトリ判別
    if ( is_dir($path) )
    {
        // ディレクトリの場合、そのディレクトリの一覧を取得するために、
        // ディレクトリハンドラを開く
        if ( $dh = opendir($path) )
        {
            // ディレクトリの中身をループ処理
            //   readdir()でディレクトリエントリを取得し、
            //   それが妥当な場合は処理開始
            while (false !== ($entry = readdir($dh)))
            {
                // 絶対パスを生成
                $realPath = realpath($path) . DIRECTORY_SEPARATOR . $entry;

                // "."と".."と自分のスクリプトファイルは省く
                if ("." !== $entry && ".." !== $entry && __FILE__ !== $realPath)
                {
                  // ファイルリストを溜め込む箱に詰む
                  $buf[] = $realPath;

                  // エントリがディレクトリだったら、再帰
                  if ( is_dir($realPath) )
                  {
                    getFileList($realPath, $buf);
                  }
                }
            }

            closedir($dh);
        }

      }

}

?>