Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Loop throw files in the first directory and see if they exist in the second.
* See tutorial here: http://tutorialspage.com/how-to-compare-two-directories-for-missing-files/
*/
$time = microtime(true); // Gets microseconds
define("MY_PATH", dirname(__FILE__));
// you can edit this like this
// define("MY_PATH", "my/custom/path/to/my/directories");
// set your custom paths
$dir[1] = MY_PATH."/dir1";
$dir[2] = MY_PATH."/dir2";
$dir[3] = $dir[1]."_processed"; // please note the folder "dir1_processed" must exist if you want to move files to it
// call the function and get the job done
echo compare_two_directories($dir);
// this next line is an example for png images, DELETE files and output message
// echo compare_two_directories($dir, ".jpg", true, true);
// see time elipsed
echo "<br />Processing took <span style='color:blue'>".round( (microtime(true) - $time), 2).'</span> seconds';
// see memory usage
echo "<br />".get_memory()." of memory were used wile processing" ;
/**
* @param array $dir: this is an array with your custom paths
* @param string $ext: File extension
* @param boleean $rename: If false, it will delete the file.
* @param boleean $output: If false, no message will be output to screen.
* @return string
*/
function compare_two_directories($dir, $ext=".jpg", $move=true, $output=true){
$files = glob( $dir[1]."/*".$ext );
$count = 0;
if($output) echo "<pre>I found this duplicate files:<br />";
foreach ($files as $file) {
$file_name = basename($file);
// check if file exists in the second directory
if(file_exists($dir[2]."/".$file_name)){
if($output) echo "$file_name";
if($move) {
rename($file, $dir[3]."/".$file_name); // move the image to folder 3.
if($output) echo " <span style='color:green'>moved</span> to ".basename($dir[3])."<br />";
} else {
unlink($file); // just delete the image
if($output) echo " <span style='color:red'>deleted</span><br />";
}
$count++;
}
}
if($output) echo "</pre>";
return "Done processing and found <span style='color:green'>$count</span> duplicated <span style='color:red; font-weight:bold;'>$ext</span> files ";
}
function get_memory()
{
$size = memory_get_peak_usage (true);
$unit = array('b','kb','mb','gb','tb','pb');
return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment