I have this script that works good, but I need to add a command for searching on all subfolders.
Example: I have a folder data and this contains more another folders... I need to search files on this folders.
$dir = 'data';
$exclude = array('.', '..', '.htaccess');
$q = (isset($_GET['q'])) ? strtolower($_GET['q']) : '';
$res = opendir($dir);
while(false !== ($file = readdir($res))) {
if(strpos(strtolower($file), $q) !== false &&!in_array($file, $exclude)) {
echo "<a href='$dir/$file'>$file</a>";
echo "<br>";
}
}
closedir($res);
you can use scandir() function
$dir = '/tmp';
$file = scandir($dir);
print_r($file);
I think this is the recursive function you are looking for :
function dir_walk($dir, $q) {
$q = trim($q);
$exclude = array('.', '..', '.htaccess');
if($dh = opendir($dir)) {
while(($file = readdir($dh)) !== false) {
if(in_array($file, $exclude)) { continue; }
elseif(is_file($dir.$file)) {
if($q === '' || strpos(strtolower($file), $q) !== false) {
echo '<a href='.$dir.$file.'>'.$dir.$file.'</a><br/>';
}
}
elseif(is_dir($dir.$file)) {
dir_walk($dir.$file.DIRECTORY_SEPARATOR, $q);
}
}
closedir($dh);
}
}
$q = (isset($_GET['q'])) ? strtolower($_GET['q']) : '';
dir_walk('/data/', $q);
Edit: data need to be the absolute path to the main dir, with ending directory separator "/data/"