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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.