如何比较2个文件夹,如果文件丢失,请采取措施?

I'm using a script for resize photos to another folder and keep the same name automaticaly found here and working great!

I have hundreds of photos in my source folder and i'm going to do this action often. I would like to optimize the time by making it resize the photos only if it sees the photos doesn't exist in the destination folder. How can we do that?

Here is the full code used:

function imageResize($file, $path, $height, $width)
{

       $target = 'smallphotos/';

       $handle = opendir($path);

       if($file != "." && $file != ".." && !is_dir($path.$file))
       {

              $thumb = $path.$file;

              $imageDetails = getimagesize($thumb);

              $originalWidth = $imageDetails[0];

              $originalHeight = $imageDetails[1];

              if($originalWidth > $originalHeight)
              {

                     $thumbHeight = $height;

                     $thumbWidth = ($originalWidth/($originalHeight/$thumbHeight));

              }
              else
              {

                     $thumbWidth = $width;

                     $thumbHeight = ($originalHeight/($originalWidth/$thumbWidth));

              }

              $originalImage = ImageCreateFromJPEG($thumb);

              $thumbImage = ImageCreateTrueColor($thumbWidth, $thumbHeight);

              ImageCopyResized($thumbImage, $originalImage, 0, 0, 0, 0, $thumbWidth, 
              $thumbHeight, $originalWidth, $originalHeight);

              $filename = $file;

              imagejpeg($thumbImage, $target.$filename, 100); 

       }

       closedir($handle);

}

    $source = "photos";

    $directory = opendir($source); 

    //Scan through the folder one file at a time

    while(($file = readdir($directory)) != false) 
    { 

           echo "<br>".$file;

           //Run each file through the image resize function

           imageResize($file, $source.'/', 640, 480);

    }   

Seems like you could just add one more condition to the resize function

if ($file != "." && $file != ".." && !is_dir($path.$file) && !is_file($target.$file) {...

This should keep it from trying to do anything if the target file already exists.

Another option would be to check if the destination file exists before calling the function

while (($file = readdir($directory)) != false) { 
    echo "<br>".$file;
    //Run each file through the image resize function (if it has not already been resized)
    if (!is_file("smallphotos/$file")) {
        imageResize($file, $source.'/', 640, 480);
    }
}