Take the 2-minute tour ×

I have a server with a lot of files inside various folders, sub-folders, and sub-sub-folders.

I'm trying to make a search.php page that would be used to search the whole server for a specific file. If the file is found, then return the location path to display a download link.

Here's what i have so far:

$search = "test.zip";
$found_files = glob("$root/*/test.zip");
$downloadlink = str_replace("$root/", "", $found_files[0]);
if (!empty($downloadlink)) {
echo "<a href=\"http://www.mydomain.com/$downloadlink\">$search</a>";

The script is working perfectly if the file is inside the root of my domain name... Now i'm trying to find a way to make it also scan sub-folders and sub-sub-folders but i'm stuck here. Any help ?

share|improve this question
You mind have better luck using the file_exists() function. php.net/manual/en/function.file-exists.php (or a mix of). –  Fred -ii- Jun 18 '13 at 4:58
doesn't tells me how to scan all sub-folders and sub-sobfolders for the file... –  Winston Smith Jun 18 '13 at 5:04
True. Have you had a look at the link messi fan put up? Seems promising. I'm dabbling with it now, and it's showing me all files in starting folder and sub-folders, but not working the way you want it to. Plus, I've got both eyes in the same socket right; needing some sleep, very soon. –  Fred -ii- Jun 18 '13 at 5:13

1 Answer 1

up vote 1 down vote accepted

There're 2 ways, code below is from here: http://thephpeffect.com/recursive-glob-vs-recursive-directory-iterator/

Use glob to do recursive search:


// Does not support flag GLOB_BRACE
function rglob($pattern, $flags = 0) {
    $files = glob($pattern, $flags); 
        $files = array_merge($files, rglob($dir.'/'.basename($pattern), $flags));
    return $files;


Use RecursiveDirectoryIterator

function rsearch($folder, $pattern) {
    $dir = new RecursiveDirectoryIterator($folder);
    $ite = new RecursiveIteratorIterator($dir);
    $files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH);
    $fileList = array();
    foreach($files as $file) {
        $fileList = array_merge($fileList, $file);
    return $fileList;

RecursiveDirectoryIterator comes with PHP5 while glob is from PHP4. Both can do the job, it's up to you.

share|improve this answer
ok but how can i use it to search for a specific file within folders/subfolders/subsubfolders and return the file's path ? –  Winston Smith Jun 18 '13 at 5:29
rsearch: var_dump(rsearch('/folder/.../', '/.*zip/')); rglob: var_dump(rglob('/folder/*/test.zip')); it returns an array of matched files. –  Tony Chen Jun 18 '13 at 5:45
can't get it to work... i tried with var_dump(rsearch('/', 'test.zip')); and also with var_dump(rsearch('$root', 'test.zip')); ... could you update your post with a code that works with my example in OP ? I want to search all folders and sub-folders for test.zip –  Winston Smith Jun 18 '13 at 6:40
@WinstonSmith It does work. if you use rsearch, $pattern param is regular expression, which is why in my example there 2 slashes wrap around. You can use rglob, which accept wildcard parameter. –  Tony Chen Jun 18 '13 at 6:45
tried var_dump(rsearch('$root', '/test.zip/')); doesnt work neither... your example search for all zip files, i want to search for a specific file (test.zip in the example, but it can also be somefile.rar or whatever.mp3) –  Winston Smith Jun 18 '13 at 7:14

Your Answer


