如何在php中将重复的文件从一个文件夹移动到另一个文件夹


How to move repeated files from folder to another in php?

我想扫描我的whatsapp图像文件夹,并将所有重复的图像移动到名为recycle bin的文件夹中,以便稍后删除它们,这是我的代码:

<?php
$dir    = 'C:'wamp'www'whatsapp';
$files = scandir($dir);
$x = 0;
foreach($files as $f1)
{
    $crc1 = strtoupper(dechex(crc32(file_get_contents("whatsapp/".$f1))));
    unset($files[$x]);
    $j = 0;
    foreach($files as $f2)
    {
        $crc2 = strtoupper(dechex(crc32(file_get_contents("whatsapp/".$f2))));
        if($crc1 == $crc2){
            rename("whatsapp/".$f2, "recycle bin/".$f2);
            unset($files[$j]);
        }
        $j++;
    }
    $x++;
}
exit('Done');

这个代码似乎可以被信任只移动重复的图像而没有任何错误吗?

我已经为您的案例编写了一个小脚本(但我还没有测试它):

<?php
$fileHashes = [];
foreach(scandir('C:'wamp'www'whatsapp') as $file){
    $fileHashes["whatsapp/".$file] = sha1(file_get_contents("whatsapp/".$file));
}
$doubles = array_diff_key($fileHashes, array_unique($fileHashes))
foreach($doubles as $file=>$hash){
    unlink($file);
}
exit('Done');