Hi Guys. I am currently in the process of reading though a php book and there is a recursion script I have been examining and trying to understand.
Basically, what this script does is allow the user to type a name of a folder/directory to search for and then output the results, giving the full path of where the file is located (if it exists) and a message telling the user the directory doesn't exist, if it doesn't exist.
So what I did was create some dummy folders and create other dummy folders inside them folders.
I used these folders to test the script.
I ran the script, searching for a folder named "images", and the following was output in the browser:
Searching for 'images' in '.' ...
The following folders matched your search:
./home/matt/images ./home/paul/images ./home/paul/yeah/haha/images ./images[/QUOTE]
Here is the script:
<html> <head> <title>Practice PHP</title> <link rel="stylesheet" type="text/css" href="common.css"> <style type="text/css"> table, td, th { border: 1px solid; } .altrow { background-color: ff0000; } </style> </head> <body> <?php define( "TOP_LEVEL_DIR", "." ); if ( isset( $_POST['posted'] ) ) { // Get the folder to search for $folderName = isset( $_POST['folderName'] ) ? $_POST['folderName'] : ""; // Search for the folder echo "<p>Searching for '$folderName' in '" . TOP_LEVEL_DIR . "' ...</p>"; $matches = array(); searchFolder( TOP_LEVEL_DIR, $folderName, $matches ); // Display any matches if ( $matches ) { echo "<h2>The following folders matched your search:</h2>\n<ul>\n"; foreach ( $matches as $match ) echo ( "<li>$match</li>" ); echo "</ul>\n"; } else { echo "<p>No matches found.</p>"; } } /** * Recursively searches a directory for a subdirectory * * @param string The path to the directory to search * @param string The subdirectory name to search for * @param stringref The current list of matches */ function searchFolder( $current_folder, $folder_to_find, &$matches ) { if ( !( $handle = opendir( $current_folder ) ) ) die( "Cannot open $current_ folder." ); while ( $entry = readdir( $handle ) ) { if ( is_dir( "$current_folder/$entry" ) ) { if ( $entry != "." && $entry != ".." ) { // This entry is a valid folder // If it matches our folder name, add it to the list of matches if ( $entry == $folder_to_find ) $matches[] = "$current_folder/$entry"; // Search this folder searchFolder( "$current_folder/$entry", $folder_to_find, $matches ); } } } closedir( $handle ); } // Display the search form ?> <form method="post" action=""> <div> <input type="hidden" name="posted" value="true" /> <label>Please enter the folder to search for:</label> <input type="text" name="folderName" /> <input type="submit" name="search" value="Search" /> </div> </form> </body> </html>
......... now getting to my confusion.
What I am struggling to understand is how the code, above, is able to output a path string as long as this:
./home/paul/yeah/haha/images
.... I don't understand how the fist 3 folders:
./home/paul/yeah/
.... are even stored in memory as a string.
I find this really mind boggling and confusing.
I mean, this code:
$entry = readdir( $handle )
....... should only be able to read one directory at a time right??
so how does:
"current_folder/$entry"
..... manage to store a string as long as:
./home/paul/yeah/haha/images
..... when it is stored in the $matches array?
Please help me!
Paul.