Page 1 of 6 123 ... LastLast
Results 1 to 10 of 54

Thread: search a directory on my site

  1. #1
    Join Date
    Jul 2007
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default search a directory on my site

    I have about 300 word/pdf files that I want to create a search box to search just that directory and pull up all results on the same page. Could someone please help me with this.

    Thanks
    Derek McIntire

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Might run a bit slow. Using a database with a precompiled list of words might be beneficial.

    PHP Code:
    <?php
    function searchfiles($search,$dir) {
    $results = array();
    $n=0;
    $opendir opendir($dir);
    while (
    $file readdir($opendir)) {
    if (
    in_array($file,array('.','..','otherfile.ext','....'))) continue;
    //leave '.' and '..', and add any other files to skip... but try to keep mostly only searched files in this directory
    if (!strpos($search,file_get_contents($dir.'/'.$file))) continue;
    $array[$n] = $file;
    $n++;
    }
    return 
    $array;
    }

    print_r(searchfiles($search,'mydocs')); //set mydocs to your directory
    //$search must be the text to match
    ?>
    The formatting will be a bit off, and the files won't be links or anything, but you should be able to figure that out.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jul 2007
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank You
    I will try it and see

  4. #4
    Join Date
    Jul 2007
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    where would I put the search box and the submit button in this script?

  5. #5
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    You would want to do something like this:

    Code:
    <?php
    function searchfiles($search,$dir) {
    $results = array();
    $n=0;
    $opendir = opendir($dir);
    while ($file = readdir($opendir)) {
    if (in_array($file,array('.','..','otherfile.ext','....'))) continue;
    //leave '.' and '..', and add any other files to skip... but try to keep mostly only searched files in this directory
    if (!strpos($search,file_get_contents($dir.'/'.$file))) continue;
    $array[$n] = $file;
    $n++;
    }
    return $array;
    }
    
    
    if (isset($_POST['search'])) {
    
    print_r(searchfiles($_POST['term'], 'mydocs')); //set mydocs to your directory
    //$search must be the text to match
    }
    
    else {
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
    Search Term: <input type="text" name="term"> <input type="submit" name="search">
    </form>
    <?php
    }
    ?>
    
    Hope this helps.
    Last edited by thetestingsite; 07-22-2007 at 02:32 AM.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  6. #6
    Join Date
    Jul 2007
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I think I know the answer to this ? but If I have php anywhere in my page do I need to save that page with a .php extention

  7. #7
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Yes, or at least set up the server to parse whatever file extension you name it to to serve as a PHP file. On most server installs though, you will want to name it as myfile.php (where myfile is the name of the file).

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  8. #8
    Join Date
    Jul 2007
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank You for your time
    DM

  9. #9
    Join Date
    Jul 2007
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I did everything and I posted it and this what I am recieving on the page that it is susposed to display

    Warning: file_get_contents(php): failed to open stream: No such file or directory in /home/content/d/e/r/derekm806/html/ordinances.php on line 69

    This is what is on Line 69 of the php file.

    if (!strpos($search,file_get_contents($ord.''.$ordinances.php))) continue;


    Could you tell me what I am doing wrong.

    DM

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You took out the slash.

    $dir.'/'.$file --> dir/file.ext
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •