...

View Full Version : readdir



stophon4
05-25-2004, 08:51 PM
if ($handle = opendir('replays')) {
while (false !== ($dir = readdir($handle))) {
if ($dir != "." && $dir != "..") {
if ($handle2 = opendir('new')) {
while (false !== ($new = readdir($handle2))) {
if ($new != "." && $new != "..") {
$file2 = $dir;
$file = $new;
$line = file($file);
$line2 = file($file2);
if($line[1] == $line2[1]){
// Could be copy, check next line
if($line[2] == $line2[2]){
// definetly copy, return error
$copy = true;
}
}else{
// replay ok
$copy = false;
}
}
}
closedir($handle2);
}
}
}
closedir($handle);
}

A problem that, (and I know it is this problem) is that the second readdir, only can read the first line of a file, and I know you think of 500 hundred other more likely reasons why it would do this but I tested it alot, and it always ends up reading only the first line of a file from that directory :p

firepages
05-26-2004, 05:07 AM
not a clue why it is not working, but that code gives me a headache ;) (readdir always does which Is why I like glob() (http://www.php.net/glob))

are you trying to check if 2 files have the same contents? if so don't open them , make a hash of them and compare...(you may have more specific requirements than a simple check but if not..)



<?
if( md5_file($file1) == md5_file($file2) ){
//duplicate file
}
?>

carl_mcdade
05-26-2004, 09:29 AM
As firepages says the code is not clear. But it looks like you are using the directory handle rather than it's contents.

What are you trying to do in plain words? Maybe we can come up with some better logic so that the code is debuggable.

stophon4
05-26-2004, 12:10 PM
I am trying to open all the files in the directory, and compare all those files with all the files in the other directory, that's what the while loops are for, they make the code execute so many times, with different files each time, understand?

carl_mcdade
05-26-2004, 12:41 PM
Are the directories Parent child or two parents? One directory contained within the other or are they side by side?

firepages
05-26-2004, 12:50 PM
file($filename) should be file($path.'/'.$filename) , e.g. you are trying to read a file that does not exist ~

break your code up a little , it makes it a little easier onthe eye (to me anyway)



<?
function compare( $hash , $dir ){
if ($handle = opendir($dir)) { /*bad -see below*/
while (false !== ($file = readdir($handle))) {
if( is_file( $dir.'/'.$file ) ){
if( md5_file( $dir.'/'.$file ) == $hash ){
closedir($handle);
return $dir.'/'.$file .'is a copy<br />';
}
}
}
closedir($handle);
return 'no duplicate found<br />';
}
}

/*directories*/
$dir1 ='dir1';
$dir2 ='dir2';
if ($handle = opendir($dir1)) {
while (false !== ($file = readdir($handle))) {
if ( is_file( $dir1.'/'.$file ) ) {
echo compare( md5_file( $dir1.'/'.$file ) , $dir2 ) ;
}
}
closedir($handle);
}
?>


the compare function is very inefficient as it keeps reading the 2nd directory evey time it is called , so , better to run it once the first time called then work on a cached array representation of the file list from then on.

stophon4
05-26-2004, 07:45 PM
No I only need it to compare lines 2 & 3

carl_mcdade
05-26-2004, 08:02 PM
Ahh! now I get it. You know firepages code is a better check than string comparison. But when using file() you have to use trim() also when comparing the strings. file() is weird in that it counts \n and whitespace as part of the string before loading it into the array. It also is not dependable because it acts differently on windows. The spaces and carriage returns get mangled somehow so you might want to clean them out before you do the comparison.

stophon4
05-26-2004, 08:29 PM
Still not the problem, the problem is it only reads line 1 of the files in the new directory...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum