simple_man_11 Posted March 3, 2007 Share Posted March 3, 2007 Can some tell me how to modify my code so that it will look through the directory and make a list of the filenames that are "like" the filename that I type in my search text box? <?php /* edit $path to the directory you want to use edit $file_types to change the file types to show */ function file_type($file) { $path_chunks = explode("/", $file); $thefile = $path_chunks[count($path_chunks) - 1]; $dotpos = strrpos($thefile, "."); return strtolower(substr($thefile, $dotpos + 1)); } $file_count = 0; $path = "./uploads/"; $file_types = array('pdf', 'jpeg', 'jpg', 'ico', 'png', 'gif', 'bmp', 'doc', 'exe', 'sql'); $p = opendir($path); while (false !== ($filelook = readdir($p))) { $files[] = $filelook; } sort($files); echo "<b> Your file results:</b><br> "; foreach ($files as $file) { $extension = file_type($file); if($file != '.' && $file != '..' && array_search($extension, $file_types) !== false ) { $file_count++; echo '<a href="'.$path.$file.'">'.$file.'</a> <br/>'; //find filename like name searched for... } } if($file_count == 0) { echo "<b>No file match your file types</b>"; } ?> Html code: <form name="form1" method="post" action="search.php"> <label> <div align="right"> <input type="text" name="filelook"> <input type="submit" name="Submit" value="Search for filename"> </div> </label> <div align="right"> <label> </div> <p> </p> <p> </p> </form> Link to comment https://forums.phpfreaks.com/topic/40953-solved-search-for-files-in-directory-by-a-like-filename/ Share on other sites More sharing options...
kenrbnsn Posted March 3, 2007 Share Posted March 3, 2007 Take a look at the glob() function. Ken Link to comment https://forums.phpfreaks.com/topic/40953-solved-search-for-files-in-directory-by-a-like-filename/#findComment-198346 Share on other sites More sharing options...
simple_man_11 Posted March 3, 2007 Author Share Posted March 3, 2007 sorry, I accidently posted twice because I couldnt find my other post ???. I got this to work except now it is showing the full path. Someone told me to use basename() to take care of that but I am having no luck, could you help me out? Link to comment https://forums.phpfreaks.com/topic/40953-solved-search-for-files-in-directory-by-a-like-filename/#findComment-198348 Share on other sites More sharing options...
The14thGOD Posted March 3, 2007 Share Posted March 3, 2007 im not sure how you have it set up but maybe try something like: $fullpath = the like search result $dispthis = str_replace("path","",$fullpath) Link to comment https://forums.phpfreaks.com/topic/40953-solved-search-for-files-in-directory-by-a-like-filename/#findComment-198410 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.