mehdi110 Posted February 25, 2008 Share Posted February 25, 2008 i used this code to Search directory <?php if ($handle = opendir('./')) { while (false !== ($file = readdir($handle))) { $filesplit = explode(".", $file); $check = $filesplit[0]; $keyword=$_GET['search']; if(ereg($keyword, $check)) { $check .= ".$filesplit[1]"; $valid[] = $check; } } for($index = 0; $index < count($valid); $index++) { echo"<br>$valid[$index]"; } closedir($handle); } ?> but it only search the main directory, not the Subdirectories.... can someone please tell how to search all subdirectories too.... Quote Link to comment Share on other sites More sharing options...
Orio Posted February 25, 2008 Share Posted February 25, 2008 Haven't tested this, but I think it should do the job. <?php //Searches recurisvely for $keyword in a given folder and it's subfolders function search_files ($keyword, $dir = ".") { $files = array(); if (!$handle = opendir($dir.'/')) return $files; while ($file = readdir($handle)) { if($file == "." || $file == "..") continue; if(is_dir($file)) $files = array_merge(search_files($file), $files); else { $no_ext = substr($file, 0, strrpos($file, '.') - strlen($file)); //remove extension if(strpos($no_ext, $keyword) !== FALSE) $files[] = $file; } } closedir($handle); return $files; } $files = search_files("keyword"); foreach($files as $file) echo $file."<br>"; ?> Orio. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.