Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9

Thread: readdir

  1. #1
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts

    readdir

    PHP Code:
    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

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 96 Times in 94 Posts
    not a clue why it is not working, but that code gives me a headache (readdir always does which Is why I like 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..)

    PHP Code:
    <?
    if( md5_file($file1) == md5_file($file2) ){
      
    //duplicate file
    }
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    Regular Coder
    Join Date
    May 2004
    Location
    sweden
    Posts
    236
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Carl McDade
    _____________
    Hiveminds Magazine
    for web publisher and community builders
    eRuby Tutorials

  • #4
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #5
    Regular Coder
    Join Date
    May 2004
    Location
    sweden
    Posts
    236
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Are the directories Parent child or two parents? One directory contained within the other or are they side by side?
    Carl McDade
    _____________
    Hiveminds Magazine
    for web publisher and community builders
    eRuby Tutorials

  • #6
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 96 Times in 94 Posts
    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)

    PHP Code:
    <?
    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 
    comparemd5_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.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #7
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No I only need it to compare lines 2 & 3

  • #8
    Regular Coder
    Join Date
    May 2004
    Location
    sweden
    Posts
    236
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Carl McDade
    _____________
    Hiveminds Magazine
    for web publisher and community builders
    eRuby Tutorials

  • #9
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Still not the problem, the problem is it only reads line 1 of the files in the new directory...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •