Jump to content

[SOLVED] search for files in directory by a like filename...


simple_man_11

Recommended Posts

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> 

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?

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.