Created
July 28, 2013 16:00
-
-
Save rudiedirkx/6099075 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$files = glob('*'); | |
$sizes = array_map('filesize', $files); | |
$count_sizes = array_count_values($sizes); | |
$doubles = count($sizes) - count($count_sizes); | |
// var_dump($doubles); | |
$double_sizes = array_filter($count_sizes, function($num) { | |
return $num > 1; | |
}); | |
// print_r($double_sizes); | |
// var_dump(count($double_sizes)); | |
$double_files = array(); | |
foreach ( $sizes as $index => $size ) { | |
if ( isset($double_sizes[$size]) ) { | |
$double_files[$index] = $files[$index]; | |
} | |
} | |
// print_r($double_files); | |
// var_dump(count($double_files)); | |
// exit; | |
function superhash($file) { | |
return filesize($file) . ':' . sha1_file($file); | |
} | |
$file_hashes = array_map('superhash', $double_files); | |
$double_files = array(); | |
foreach ( $file_hashes as $index => $hash ) { | |
$double_files[$hash][] = $files[$index]; | |
} | |
$double_files = array_filter($double_files, function($files) { | |
return count($files) > 1; | |
}); | |
$sorted_double_files = array_map(function($files) { | |
usort($files, function($a, $b) { | |
$a_rb = strpos($a, 'random-') !== FALSE; | |
$b_rb = strpos($b, 'random-') !== FALSE; | |
if ( $a_rb XOR $b_rb ) { | |
return $a_rb ? -1 : 1; | |
} | |
return strlen($a) - strlen($b); | |
}); | |
return $files; | |
}, $double_files); | |
print_r($sorted_double_files); | |
var_dump(count($sorted_double_files)); | |
foreach ( $sorted_double_files as $hash => $files ) { | |
print_r(array_map('unlink', array_slice($files, 1))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment