Jump to content

search for files in directory by a like filename...


simple_man_11

Recommended Posts

<?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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.