function find_duplicate_files() {
$names = scandir_recursive( 'D:\Main' );
$files = array();
foreach( $names as $name ) {
if( count( $name ) > 1 ) {
$files[] = $name;
}
}
print_r( $files );
}
Function scandir_recursive() recursively parses the specified directory tree and creates an associative array whose keys are the file names found in all subdirectories, and whose values are the corresponding paths.
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;
}
// It will output like
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
)
)
From which we can identify which are duplicate files. It is useful when your code base is having large number of files. ( And I have used it a lot for finding duplicate music mp3 files :P )