function find_duplicate_files() {
$names = scandir_recursive( 'D:\Main' );
$files = array();
foreach( $names as $name ) {
if( count( $name ) > 1 ) {
$files[] = $name;
}
}
print_r( $files );
}
関数 scandir_recursive() は、指定されたディレクトリ ツリーを再帰的に解析し、キーがすべてのサブディレクトリで見つかったファイル名であり、値が対応するパスである連想配列を作成します。
function scandir_recursive( $dir, &$result = array() ) {
$dir = rtrim($dir, DIRECTORY_SEPARATOR);
foreach ( scandir($dir) as $node ) {
if ($node !== '.' and $node !== '..') {
if (is_dir($dir . DIRECTORY_SEPARATOR . $node)) {
scandir_recursive($dir . DIRECTORY_SEPARATOR . $node, $result);
} else {
$result[$node][] = $dir . DIRECTORY_SEPARATOR . $node;
}
}
}
return $result;
}
// のように出力されます
Array
(
[0] => Array
(
[0] => D:\Main\Games\troy.php
[1] => D:\Main\Games\Child Games\troy.php
[2] => D:\Main\Games\Sports\troy.php
)
[1] => Array
(
[0] => D:\Main\index.php
[1] => D:\Main\Games\index.php
)
)
そこから、重複ファイルを特定できます。コードベースに多数のファイルがある場合に便利です。(そして、重複した音楽mp3ファイルを見つけるためによく使用しました:P )