Jump to content

Search form to search a directory


UnrealEgg

Recommended Posts

I would like to try and make a search form that searches a directory of files.

 

E.g. User inputs "video" in search bar, the search then returns:

 

Bear_video.avi

video butterfly.wmv

video392.mpeg

 

Also, once it finds the files they need to link to: view.php?url=name-of-file.wmv

 

I asked on a different forum and they said to use glob() or strpos() but I have no idea how to set it up.

 

Any ideas  ???

Link to comment
https://forums.phpfreaks.com/topic/145891-search-form-to-search-a-directory/
Share on other sites

I have had a go at trying to make one and here is what I have so far. It inst working I don't think.

 

<form name="search-form" id="search-form" method="post" action="searchphp.php" enctype="multipart/form-data" style="height: 103px"> 
  <legend>
		<dl class="style2"> 
    			<dd class="style1"> 
      <input tabindex="1" accesskey="b" name="search" type="text" id="search" /> 
    </dd>
  </dl>
  <input tabindex="2" accesskey="l" type="submit" name="cmdsearch" value="Search" /> 
</form>

 

php:

 

<?php
  if(isset($_POST['cmdsearch']))
{
      $search = $_POST['cmdsearch'];
foreach (glob("video/$search") as $filename) {  
  echo $filename; echo "<br />";  
}}  
  else{
  echo  "<p>Please enter a search query.</p>";
  }
?>

Hi,

 

Good on you for having a go!! :-)

 

I dont know much about glob() but.. if you take a look at what you have already.

//a textbox for the search word - called search, great!
<input tabindex="1" accesskey="b" name="search" type="text" id="search" />

// A button called cmdsearch to submit the form also great!! - I would leave the name as submit to save confsusion..
<input tabindex="2" accesskey="l" type="submit" name="cmdsearch" value="Search" />

 

Ok so you have a basic but functioning html form. lets look at the PHP

<?php
  if(isset($_POST['cmdsearch'])) //Checking the submit button was pressed. Great!!
{

     // $search = $_POST['cmdsearch']; //assinging the variable for search keyword to the value of the button - Not so great
//Lets change it to
$search = $_POST['search'];
foreach (glob("video/$search") as $filename) {
  echo $filename; echo "<br />";
}}
  else{
  echo  "<p>Please enter a search query.</p>";
  }
?>

 

See how that goes its untested.

 

 

I tried that, but its just bringing up a blank screen as before  ???

 

If you want to know where I got the script from, go here:

http://www.bitrepository.com/web-programming/php/some-ways-to-scan-a-directory.html

 

You may understand it more than what I do :)

I got it working now, but the problem is you have to search like the following:

*.wmv or Bear*

 

Also, if I search bear* it does not bring anything up so it must be case sensitive. Any ideas how I can fix this so its not case sensitive and users don't have to put the * in?

 

EDIT:

If I search Bear.wmv it brings up the video as well.

Fixed the * problem pretty easily by just adding a * after $search:

 

   foreach (glob("$search*") as $filename) {

 

Now when I just Search Bear, it brings up Bear.wmv  :D

 

Last problems:

Is there any way I can fix the case sensitive issue?

Also, is there a way I can ban search phrases such as index, php, e.t.c.

A guy on another forum suggested this:

 

if(isset($_POST['cmdsearch']))
{
    $search = $_POST['search'];
    if (@get_magic_quotes_gpc())
    {
        $search = stripslashes($search);
    }
    foreach (glob("*") as $filename)
    {
        if (false !== stripos($filename, $search))
        {
            echo $filename; echo "<br />";
        }
    }
}
else
{
    echo  "<p>Please enter a search query.</p>";
}  

 

And it works! Only last problem now is to block certain key words such as php, index e.t.c.

 

Any ideas?

once again untested



if(isset($_POST['cmdsearch']))
{
    
    $blacklist = array('index','php'); // keep adding them
   
    $search = $_POST['search'];
    $error = array();
    foreach ($blacklist as $key) {
        
        if ($search == $key){
            
            $error[] = $search . " is a banned keyword";
        }
    }
    if (count($error) > 0){
        foreach ($error as $message){
            echo $message . "<br />";
        }
    }
    else {
    
        if (@get_magic_quotes_gpc())
        {
            $search = stripslashes($search);
        }
        foreach (glob("*") as $filename)
        {
            if (false !== stripos($filename, $search))
            {
                echo $filename; echo "<br />";
            }
        }
    }
}
else
{
    echo  "<p>Please enter a search query.</p>";
}

 

 

 

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.