html - PHP Search Sub-Directory for a specific file -
i've been looking solution problem. have website has many files in many sub-directories (all in 1 main directory). need search function can search part of name of file , return back. need function not case sensitive (i.e. if file name caps , user searches lower case should still find file). function have below works 1 directory , not work recursively.
please me modify code work multiple directories.
<?php $dirname = "./office precedents/";//directory search in. *must have trailing slash* $findme = $_post["search"]; $dir = opendir($dirname); while(false != ($file = readdir($dir))){ //loop every item in directory. if(($file != ".") , ($file != "..") , ($file != ".ds_store") , ($file != "search.php")) { //exclude these files search $pos = stripos($file, $findme); if ($pos !== false){ $thereisafile = true;//tell script found. //display search results links. echo'<a href="' . $dirname . $file . '">' . $file . '</a><br>'; }else{ //leave blank } } } if (!isset($thereisafile)){ echo "nothing found.";//tell user nothing found. echo '<img src="yourimagehere.jpg"/>';//display image, when nothing found. } ?>
give try:
<?php //--- directories $dirname = ''; $findme = "*.php"; $dirs = glob($dirname.'*', glob_onlydir); $files = array(); //--- search through each folder file //--- append results $files foreach( $dirs $d ) { $f = glob( $d .'/'. $findme ); if( count( $f ) ) { $files = array_merge( $files, $f ); } } if( count($files) ) { foreach( $files $f ) { echo "<a href='{$dirname}/{$f}'>{$f}</a><br>"; } } else { echo "nothing found.";//tell user nothing found. echo '<img src="yourimagehere.jpg"/>';//display image, when nothing found. } ?>
Comments
Post a Comment