Thursday, July 09, 2009

Find and Replace Missing Files

With using this PHP Class you can be able to check the count of files in 2 locations and find the missing files in destination location compare with source location.

1) Identify the missing files.
2) Count the missing files from source location and update it to destination.
3) If it is exist compare the file sizes and update un-match files.
4) All the details are saved in text file.



// | File         : matchdir.php
// | Created by   : Lahiru Manchanayake
// | Created Date : 20/06/2009
// | Modified     :

class matchDir {

public $pathDir1 ;
public $pathDir2 ;
public $f2 ;
public $file2 ;
public $missingCounter = 0 ;
public $counter = 0 ;

function matchDir($pathDir1 , $pathDir2)
{
$this -> pathDir1 = $pathDir1 ;
$this -> pathDir2 = $pathDir2 ;
$this -> f2 = opendir($this -> pathDir2);
}

function match()
{
while($this -> file2 = readdir($this -> f2))
{
if(is_file($this -> pathDir2.$this -> file2))
{
if (!file_exists($this -> pathDir1.$this -> file2))
{
 echo $this -> file2.'Not Exists';

 $f3 = fopen("lahiru.txt",'a+') ; 
 fwrite($f3,$this -> file2); 
 $this -> missingCounter++; 
 fclose($f3);

  if (copy($this -> pathDir2.$this -> file2 , $this -> pathDir1.$this -> file2))
  {
   echo "Copied";
  }else{
   echo "Faile to copy";
  } 
} else{
 echo $this -> file2.' Exists ';

  if (filesize($this -> pathDir2.$this -> file2) > filesize($this -> pathDir1.$this -> file2))
  {
   copy($this -> pathDir2.$this -> file2 , $this -> pathDir1.$this -> file2);
   echo "Updated";
  }else{
   echo "Faile to update";
  }
}
}
$this -> counter++ ;
}
closedir($this -> f2);
echo $this -> counter .'  Total Files'  ;
echo $this -> missingCounter .'  Missing Total Files'  ;
}
}

// distination
$pathDir1 = "folder2/";

// source
$pathDir2 = "folder1/";

$obj =new matchDir($pathDir1 , $pathDir2);
$obj -> match();

No comments: