- Do files exist in both directories?
- Were they both modified at the same time?
- Are the contents identical?
This function returns a 3D array containing a list of all differences between the contents of the two directories...
Please see right hand column before copying >
//SAMPLE USAGE:
print_r(returnDirDifferences("folder/subfolder","folder2/subfolder2/other");
function returnDirDifferences($dir1, $dir2='.'){
if(!$dir1){return(false);}
$ignore = array('.', '..');
$returnarray=array();
$dh = @opendir($dir1);
while(false !== ($file = readdir($dh))){ // Loop through the directory
if(!in_array($file, $ignore)){
if(is_dir("$dir1/$file")){
if(is_dir("$dir2/$file")){// Its a directory, so keep reading down:
$tempresults = returnDirDifferences("$dir1/$file","$dir2/$file");
if($tempresults[0]){
$returnarray = array_merge($returnarray,$tempresults);
}
}else{
array_push($returnarray,array('path1'=>"$dir1/$file",'path2'=>0,'status'=>"non-existing"));
}
} else {
$tempresults = returnFileDifferences("$dir1/$file","$dir2/$file");
if($tempresults){array_push($returnarray,$tempresults);}
}
}
}
closedir($dh);
//return 3D array of differences or false if there are none:
if($returnarray[0]){return($returnarray);}else{return(false);}
}
function returnFileDifferences($path1,$path2){
$path1=(file_exists($path1) ? $path1 : 0);
$path2=(file_exists($path2) ? $path2 : 0);
if(!$path1 || !$path2){//check existance:
return array('path1'=>$path1,'path2'=>$path2,'status'=>'non-existing');
}else if(filemtime($path1)!=filemtime($path2)){//check file modified time:
return array('path1'=>$path1,'path2'=>$path2,'status'=>'modified');
}else if (strtoupper(dechex(crc32(file_get_contents($path1))))!=strtoupper(dechex(crc32(file_get_contents($path2))))){ //check contents:
return array('path1'=>$path1,'path2'=>$path2,'status'=>'contents');
}//no differences:
return false;
}
11 comments:
It turns out PHP alters the modified time on files when moving them so checking the modified time in this case is not accurate to tell if two files are the same. To fix this, simply remove these two lines from the function:
else if(filemtime($path1)!=filemtime($path2)){//check file modified time:
return array('path1'=>$path1,'path2'=>$path2,'status'=>'modified');
}
Recursive results didn't seem to make it up the chain, so I changed this line
array_merge($returnarray,$tempresults);
to
$returnarray = array_merge($returnarray,$tempresults);
Now, recursive results show up in the final array.
Excellent function, by the way.
Best Regards,
Marc
Thanks Marc - looks like this is a compatibility issue with PHP5. I've updated the function above but anyone using PHP4 will likely need to revert to the old line:
array_merge($returnarray,$tempresults);
You can check filesize first; then crc32/md5/sha. I would not rely on dates.
20180623xiaoke
polo ralph lauren outlet
air max 2018
cheap jordans
canada goose jackets
coach outlet store online
dsquared2 jeans
cheap jordan shoes
oakley sunglasses wholesale
michael kors outlet clearance
coach outlet store online
pandora
bape hoodie
pandora outlet
stephen curry shoes
golden goose
jordan 12
canada goose outlet
kobe byrant shoes
yeezy boost 350 v2
bape outlet
It turns out PHP alters the modified time on files when moving them so checking the modified time in this case is not accurate to tell if two files are the same. To fix this, simply remove these two lines from the function:
else if(filemtime($path1)!=filemtime($path2)){//check file modified time:
return array('path1'=>$path1,'path2'=>$path2,'status'=>'modified');
}
We are a reliable and afforadable trucking comapany.
안동출장샵
안동출장샵
전주출장샵
무주출장샵
전주출장샵
영주출장샵
군산출장샵
광양출장샵
광양출장샵
상주출장샵
진안출장샵
상주출장샵
전북출장샵
전북출장샵
세종출장안마
총판출장안마
총판출장안마
총판출장안마
고고출장안마
심심출장안마
제주출장안마
제주도출장안마
Post a Comment