Exemplo n.º 1
0
<?php

echo "Input path: ";
$path = trim(fgets(STDIN));
if (!is_dir($path)) {
    exit('Error: ' . $path . ' is not a directory. ' . PHP_EOL . 'Exit' . PHP_EOL);
}
$files = new \DuplicateFinder($path);
$files->findDuplicates();
class DuplicateFinder
{
    private $path;
    private $resultFileName;
    private $processUnixHiddenFiles;
    private $files = array();
    public function __construct($path, $processUnixHiddenFiles = false, $resultFileName = 'duplicates.txt')
    {
        $this->path = $path;
        $this->processUnixHiddenFiles = $processUnixHiddenFiles;
        $this->resultFileName = $resultFileName;
    }
    public function findDuplicates()
    {
        echo 'Start' . PHP_EOL;
        $this->processPath($this->path)->removeUnique()->hashCheck()->removeUnique()->byteCheck()->outputResult();
    }
    // private
    private function processPath($path)
    {
        if ($handle = @opendir($path)) {
            while ($file = readdir($handle)) {
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
// Autoload required files
define('ROOT', dirname(__FILE__));
require_once ROOT . '/components/autoload.php';
// Directory for search
$directory = ROOT . "/scandir/";
if (isset($argv[1])) {
    $directory = $argv[1];
}
try {
    // Get all files
    $dataProvider = new DataProvider(new FilesystemProvider($directory));
    $filesArray = $dataProvider->getFiles();
    // Instantiate DuplicateFinder with specified processors and retrieve duplicates
    $duplicateFinder = new DuplicateFinder($filesArray);
    $duplicateFinder->attachProcessor(new FilesizeProcessor());
    $duplicateFinder->attachProcessor(new HashProcessor('adler32'));
    $duplicateFinder->attachProcessor(new HashProcessor('md5'));
    // You can run any number of hash processors against different algorithms
    //    $duplicateFinder->attachProcessor(new HashProcessor('crc32'));
    $duplicates = $duplicateFinder->run();
    // Now you are free to deal with $duplicates. Sure, I can save it to the file
    $text = '';
    foreach ($duplicates as $item) {
        $text .= $item . PHP_EOL;
    }
    file_put_contents('duplicates.txt', $text);
    echo 'Success: duplicates paths written to duplicates.txt' . PHP_EOL;
    $time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
    echo 'time: ' . $time . PHP_EOL;