Searching File Within Directory Recursively Using PHP

Recently I had to find my file stored somewhere in my server. Although I can do this easily using the built-in feature in cPanel File Manager, but what I need is to search file programaticallyusing a script. So, I created this function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function find_file($dirs,$filename,$exact=false){
 
    $dir = @scandir($dirs);
    if(is_array($dir) AND !empty($dir)){
    foreach($dir as $file){
    if(($file !== '.') AND ($file!=='..')){
    if (is_file($dirs.'/'.$file)){
        $filepath =  realpath($dirs.'/'.$file);
 
        if(!$exact){
            $pos = strpos($file,$filename);
            if($pos === false) {
            }
            else {
                if(file_exists($filepath) AND is_file($filepath)){
                echo str_replace($filename,'<span style="color:red;font-weight:bold">'.$filename.'</span>',$filepath).' ('.round(filesize($filepath)/1024).'kb)<br />';
                }
            }
        }
        elseif(($file == $filename)){
 
            if(file_exists($filepath) AND is_file($filepath)){
                echo str_replace($filename,'<span style="color:red;font-weight:bold">'.$filename.'</span>',$filepath).' ('.round(filesize($filepath)/1024).'kb)<br />';
            }
        }
    }
    else{
        find_file($dirs.'/'.$file,$filename,$exact);
    }
    }
    }
    }
}
function find_file($dirs,$filename,$exact=false){

	$dir = @scandir($dirs);
	if(is_array($dir) AND !empty($dir)){
	foreach($dir as $file){
	if(($file !== '.') AND ($file!=='..')){
	if (is_file($dirs.'/'.$file)){
		$filepath =  realpath($dirs.'/'.$file);

		if(!$exact){
			$pos = strpos($file,$filename);
			if($pos === false) {
			}
			else {
				if(file_exists($filepath) AND is_file($filepath)){
				echo str_replace($filename,'<span style="color:red;font-weight:bold">'.$filename.'</span>',$filepath).' ('.round(filesize($filepath)/1024).'kb)<br />';
				}
			}
		}
		elseif(($file == $filename)){

			if(file_exists($filepath) AND is_file($filepath)){
				echo str_replace($filename,'<span style="color:red;font-weight:bold">'.$filename.'</span>',$filepath).' ('.round(filesize($filepath)/1024).'kb)<br />';
			}
		}
	}
	else{
		find_file($dirs.'/'.$file,$filename,$exact);
	}
	}
	}
	}
}

The function accepts three parameters:

  1. $dir (string), absolute path of the directory to start the search, to use current directory use dirname(__FILE__).
  2. $filename (string), the keyword to search for.
  3. $exact (bool), true/false, default is false. If you set it to true it will find the exact ‘index.php’ not ‘anotherindex.php’ for keyword ‘index.php’.

How to use this function?

This will find all file name contains content within current directory and all it’s sub directories.

1
2
< ?php find_file(dirname(__FILE__),'content');
?>
< ?php find_file(dirname(__FILE__),'content');
?>

And the search result will be like this:

Search file using PHP functions

Search file using PHP functions

Another Approach

Another way to search file in directory is using PHP glob function, as written here.

$10.99 .CO Domains!

Incoming search terms:

  • scandir php
  • php scandir
  • php scandir recursive
  • search pictures using pictures
  • best recursive scandir filesize php
  • recursive search file scandir
  • recursive scandir php code
  • recursive file find
  • recursive file explorer php
  • php sort
  • Prahastu

    top