1
\$\begingroup\$

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?

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Why would this pseudo code be a better algorithm than what the asker currently has? (Please add some explanation) \$\endgroup\$
    – Pimgd
    Feb 6, 2015 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.