simple_man_11 Posted February 28, 2007 Share Posted February 28, 2007 <?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 !== ($filename = readdir($p))) { $files[] = $filename; } 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>"; } ?> instead of it listing all the files in the directory is there a way I can submit a html form and have this code give me a list back of a like file name? Example: if the list of files are: specialcode.pdf myfile.doc mysql.doc sports.pdf and I am only looking for a file name that I am not sure what the total name is but I know it is something like code.. so I only want the results of the specialcode.pdf in the result list. Link to comment https://forums.phpfreaks.com/topic/40446-search-for-files-in-directory-by-a-like-filename/ Share on other sites More sharing options...
btherl Posted February 28, 2007 Share Posted February 28, 2007 The simple way is with glob() $files = glob("{$path}*{$like}*"); That should give you the file "./uploads/specialcode.pdf". You may want to strip off the path afterwards, using basename() Link to comment https://forums.phpfreaks.com/topic/40446-search-for-files-in-directory-by-a-like-filename/#findComment-195776 Share on other sites More sharing options...
simple_man_11 Posted March 3, 2007 Author Share Posted March 3, 2007 I got it to work great, thanks for your help. I am trying to get the path out of it like you was stating, could you give me a little more hint on how I can get this accomplished with basename() ? Link to comment https://forums.phpfreaks.com/topic/40446-search-for-files-in-directory-by-a-like-filename/#findComment-198347 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.