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:
- $dir (string), absolute path of the directory to start the search, to use current directory use dirname(__FILE__).
- $filename (string), the keyword to search for.
- $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:
Another Approach
Another way to search file in directory is using PHP glob function, as written here.
Incoming search terms:
- php scandir
- recursive scandir php
- scandir recursive
- scandir php
- recursive directory and file list usign scandir()
- php searching file
- php search file in directory
- php search file directory
- php scandir()
- @scandir php
-
Prahastu

