Jump to content

Text File Search Across muliple directories


ricbax

Recommended Posts

Is it possible to scan multiple directories/subdirectories to locate files with php even if they are not on the same server?

 

So for example, I have 50+ flat text files all with the name "phone.txt" but they are scattered across a labyrinth of directories.

 

http://server6.domain.com/clients/data/March_26_2004_1000_PM/Company1/phones.txt

 

http://server6.domain.com/clients/data/August_6_2005_800_PM/New/Company56/phones.txt

 

I need a way to locate and consolidate all these numbers fast, is it possible?

This is actually a code that I have that I've compiled that works for me :-D Iteration.

 

(this is only same-server side). file_get_contents() will do outside files... Sorry o_o

 

<?php
function getDirectory( $path , $file_to_find){

    $dh = @opendir( $path );
    // Open the directory to the handle $dh
            $directory[] = $path;    
    while( false !== ( $file = readdir( $dh ) ) ){
    // Loop through the directory (this checks to see if the next file exists, otherwise it's the end of the directory)
   
            if($file_to_find == $file){
            return $directory;
            }
     
            if( is_dir( "$path/$file" ) ){
            // Its a directory, so we need to keep reading down...
            
                echo "<strong>$spaces $file</strong><br />";
                getDirectory( "$path/$file" , $file_to_find);
                // Re-call this same function but on a new directory.
                // this is what makes function recursive.
            
            }
            
            }
        
        }
    
    }
    
    closedir( $dh );
    // Close the directory handle

}

?>

 

Maybe this will be of some help to you.

 

$directory_array = getDirectory("/","Company1/phones.txt");
$directory = implode(">>",$directory_array);

 

What one of the problems for this script is that if you chose to find phones.txt, it will find the first instance of it, so if you did Company1/phones.txt, it will find that path with the correct file... It will return this instance in an array, what I provided is an example of making that array into an interesting breadcrumb trail.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.