If you want to find any given file inside a folder and its subfolders, you can do that with the code below.
<?php //=========== find a file based on wildcard in a dir recursively =========== clearstatcache(); $sourcepath = "/path_to_folder"; //eg. dirname(__FILE__); // Replace \ by / and remove the final / if any $root = ereg_replace( "/$", "", ereg_replace( "[\\]", "/", $sourcepath )); // Search for text files $results = m_find_in_dir( $root, "index.html" ); if( false === $results ) { echo "'{$sourcepath}' is not a valid directory\n"; } else { echo "<pre>"; print_r( $results ); echo "</pre>"; } /** * Search for a file maching a regular expression * * @param string $root Root path * @param string $pattern POSIX regular expression pattern * @param boolean $recursive Set to true to walk the subdirectories recursively * @param boolean $case_sensitive Set to true for case sensitive search * @return array An array of string representing the path of the matching files, or false in case of error */ function m_find_in_dir( $root, $pattern, $recursive = true, $case_sensitive = false ) { $result = array(); if( $case_sensitive ) { if( false === m_find_in_dir__( $root, $pattern, $recursive, $result )) { return false; } } else { if( false === m_find_in_dir_i__( $root, $pattern, $recursive, $result )) { return false; } } return $result; } /** * @access private */ function m_find_in_dir__( $root, $pattern, $recursive, &$result ) { $dh = @opendir( $root ); if( false === $dh ) { return false; } while( $file = readdir( $dh )) { if( "." == $file || ".." == $file ){ continue; } if( false !== @ereg( $pattern, "{$root}/{$file}" )) { $result[] = "{$root}/{$file}"; } if( false !== $recursive && is_dir( "{$root}/{$file}" )) { m_find_in_dir__( "{$root}/{$file}", $pattern, $recursive, $result ); } } closedir( $dh ); return true; } /** * @access private */ function m_find_in_dir_i__( $root, $pattern, $recursive, &$result ) { $dh = @opendir( $root ); if( false === $dh ) { return false; } while( $file = readdir( $dh )) { if( "." == $file || ".." == $file ){ continue; } if( false !== @eregi( $pattern, "{$root}/{$file}" )) { $result[] = "{$root}/{$file}"; } if( false !== $recursive && is_dir( "{$root}/{$file}" )) { m_find_in_dir__( "{$root}/{$file}", $pattern, $recursive, $result ); } } closedir( $dh ); return true; } ?>
This will return any files, also ones based on wildcards, such as *.txt, inde?.html, etc.