ricbax Posted October 28, 2007 Share Posted October 28, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/75094-text-file-search-across-muliple-directories/ Share on other sites More sharing options...
trq Posted October 28, 2007 Share Posted October 28, 2007 The urls you have given would suggest the files are on the same server. If so, glob may be of some use to you. Quote Link to comment https://forums.phpfreaks.com/topic/75094-text-file-search-across-muliple-directories/#findComment-379786 Share on other sites More sharing options...
ricbax Posted October 28, 2007 Author Share Posted October 28, 2007 I forgot to mention that the scan.php would be located on http://server1.domain.com/scan.php Quote Link to comment https://forums.phpfreaks.com/topic/75094-text-file-search-across-muliple-directories/#findComment-379792 Share on other sites More sharing options...
kratsg Posted October 28, 2007 Share Posted October 28, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/75094-text-file-search-across-muliple-directories/#findComment-379793 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.