Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.