I want to scan my whatsapp images folder and move all the repeated images to folder named recycle bin to delete them later:
<?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');
Does this code seems to be trusted to move only the repeated images without any mistakes?
1 Answer 1
Maybe you try with this pseudocode:
- get files list from directory
- declare array for crc
- for each file in directory
- check if the mime content type is correct
- get crc for file
- check if this crc is in array for crc
- if is in array move it
- else push crc into array
Thanks Pimgd. You are right. I should write it on the beginning. First of all there is only one loop, so if you have a lot of files, it will be faster. Second, it checks content file, so you will not touch not image files. Third, the code will be more clear.
-
2\$\begingroup\$ Why would this pseudo code be a better algorithm than what the asker currently has? (Please add some explanation) \$\endgroup\$Pimgd– Pimgd2015年02月06日 11:32:13 +00:00Commented Feb 6, 2015 at 11:32