Page 5 of 6 FirstFirst ... 3456 LastLast
Results 41 to 50 of 54

Thread: search a directory on my site

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

    Default

    I'm not sure how that's related. This will search the source code of those files. .doc format is a big mess, but it does include the actual text in the file too. It might match some of the system stuff too, like if you were to search an html page for the word "body". PDF is a mess, though, and it might not work at all. I'm not sure how this would work.
    What I supplied would work for .txt files. It should *mostly* work for .doc and .pdf too. Can't hurt to try.

    I'm not sure what you mean about a form.

    You should just make a form somewhere. That's not going to magically appear.

    $search is the variable used.

    At the top of your script, you could put:
    $search = $_GET['search'];

    And then you can use mypage.php?search=termhere

    So you could link to the page like that or do a form like:
    <form method="get" action="mypage.php"><input name="search" type="text"></form>
    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

  2. #42
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    What I supplied would work for .txt files. It should *mostly* work for .doc and .pdf too. Can't hurt to try.
    A word/PDF file is compressed/encoded in a different format. So you'll just see random ASCII characters or something. Perhaps Wingdings.

    You could try making your documents in HTML, and I'm about to finish off a nice search thing. djr33's one searches through quite alot of things, like <title> and PHP tags etc. So that could be a potential security risk.
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

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

    Default

    Quote Originally Posted by tech_support View Post
    A word/PDF file is compressed/encoded in a different format. So you'll just see random ASCII characters or something. Perhaps Wingdings.

    You could try making your documents in HTML, and I'm about to finish off a nice search thing. djr33's one searches through quite alot of things, like <title> and PHP tags etc. So that could be a potential security risk.
    So Your saying I need to go another route or what I am kinda confused now?!?!?!

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

    Default

    A word/PDF file is compressed/encoded in a different format. So you'll just see random ASCII characters or something. Perhaps Wingdings.
    This is true with PDFs, though if you look through them some of the data seems to be in English. I don't know much about the format, though.
    With word docs, from what I've seen, they are simply like html, just a ton of complex code, but with the actual text embedded in it.

    potential security risk
    No. It would be a mess, but it wouldn't be risking security, unless you were searching PHP files and other things that can't be served.



    As for what you should do with this, I say create a database and search that. Much faster, and easier, and it wouldn't run into any filetype issues. Take the text from the documents and put it into the DB, or something like that.
    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

  5. #45
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Quote Originally Posted by derekm View Post
    So Your saying I need to go another route or what I am kinda confused now?!?!?!
    Hang on... help is on it's way... i'll code as fast as I can...
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  6. #46
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Alright. Done.
    PHP Code:
    <?php
    function searchfiles($q,$dir)    {
        
    //Add the extensions you want to search through here.
        
    $exts = array('.php','.html','.htm','.docx','.doc','.pdf');
        
    //Enter the files that you DON'T want to search through, eg. this page.
        
    $exceptions = array('somefilethatyoudontwant',basename($_SERVER['PHP_SELF']));
        
    //Sets the beginning of the search results and outputs it.
        
    echo '<h2>Your Search Results</h2><ol>';
        
    flush();
        
    //Opens the directory
        
    $handle opendir($dir);
        
    //Starts a loop through the directory to list the files
        
    while ($file readdir($handle))    {
            
    //Check if the file is an exception
            
    if (in_array($q,$exceptions))    {
                continue;
            }
            
    //Check if it's showing '.' meaning this directory (the parent directory) or the directory above it (..)
            
    else if ($file == '.' || $file == '..')    {
                continue;
            }
            
    //Loops through all the extensions
            
    foreach ($exts as $extension)    {
                
    //Checks if it's an allowed file extension (eg. Word Documents, HTML Files etc.)
                
    if (strpos($file,$extension))    {
                    
    //Gets the file contents...
                    
    $c file_get_contents($dir.'\\'.$file);
                    
    //See if there's a match...
                    
    if (strpos($c,$q))    {
                        
    //Adds the URL
                        
    echo '<li><a href="'.$dir.''.$file.'" title="'.$file.'">'.getTitle($c,$file).'</a><br>';
                        
    //Gets the body content
                        
    echo getBody($c);
                        
    //Ends the list item
                        
    echo'</li>';
                        
    //Outputs it. This allows for the item to be displayed while the page is still loading. Good for when handling large files.
                        
    flush();
                    }
                }
                
    //Or else it'll continue through the loop.
                
    else    {
                    continue;
                }
            }
        }
        
    //And close the list.
        
    echo '</ol>';
    }
    //Tool: stripPHP($c)
    //Strips the PHP stuff for you so you can't search it.
    function stripPHP($c)    {
        if (
    strpos($c,'<?php') != FALSE)    {
            
    $php '';
            
    $php explode('<?php',$c);
            
    $php[1] = explode('?>',$php[1]);
            
    $c str_replace($php[1][0],'',$c);
            
    $c str_replace('<?php?>','',$c);
            return 
    $c;
        }
        else    {
            return 
    $c;
        }
    }
    //Tool: chopParagraph($str)
    //Chops the long content.
    function chopParagraph($str)    {
        
    $words 20;
        
    $chopped '';
        
    $str explode(' ',$str);
        if (
    count($str)>30)    {
            for (
    $i=0$i <= $words$i++)    {
                
    $chopped.=$str[$i].' ';
            }
            return 
    $chopped.'...';
        }
        else    {
            return 
    $str[count($str)-1];
        }
    }
    //Tool: getBody($content)
    //Gets the body content.
    function getBody($content)    {
        
    $content stripPHP($content);
        if (
    strpos($content,'<body>'))    {
            
    $content explode('<body>',$content);
            
    $content[1] = explode('</body>',$content[1]);
            
    $returner strip_tags($content[1][0]);
        }
        else    {
            
    $returner filterWeirdStuff($content); //For Word Documents...
        
    }
        return 
    chopParagraph($returner);
    }
    //Tool: getTitle($content,$filename)
    //Gets the title of the page. If there's no title, it'll display the page name.
    function getTitle($content,$filename)    {
        
    $content stripPHP($content);
        if (
    strpos($content,'<title>'))    {
            
    $content explode('<title>',$content);
            
    $content[1] = explode('</title>',$content[1]);
            return 
    htmlspecialchars($content[1][0]);
        }
        else    {
            return 
    $filename;
        }
    }
    //Tool: filterWeirdStuff($str)
    //Filters the ???????????????????***3%^#$^4 you see when trying to get the file contents of Word Documents. Ugh.
    function filterWeirdStuff($str)    {
        
    $str explode(' ',$str);
        unset(
    $str[0]);
        unset(
    $str[1]);
        
    $str implode(' ',$str);
        return 
    '...'.$str;
    }

    if (isset(
    $_POST['Submit'],$_POST['term'],$_GET['search']))    {
        
    searchfiles($_POST['term'], './');
    }
    else {
    ?>
    <form action="?search=true" method="post">
    <p>
      <label>Search: <input type="text" name="term"></label>
    </p>
    <p>
      <label><input type="submit" name="Submit" value="Search"></label>
    </p>
    </form>
    <?php
    }
    ?>
    Try check and see if it works for Acrobat Documents. I've got a tonne of homework to do, so I couldn't check it right now.
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

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

    Default

    Quote Originally Posted by tech_support View Post
    Alright. Done.
    PHP Code:
    <?php
    function searchfiles($q,$dir)    {
        
    //Add the extensions you want to search through here.
        
    $exts = array('.php','.html','.htm','.docx','.doc','.pdf');
        
    //Enter the files that you DON'T want to search through, eg. this page.
        
    $exceptions = array('somefilethatyoudontwant',basename($_SERVER['PHP_SELF']));
        
    //Sets the beginning of the search results and outputs it.
        
    echo '<h2>Your Search Results</h2><ol>';
        
    flush();
        
    //Opens the directory
        
    $handle opendir($dir);
        
    //Starts a loop through the directory to list the files
        
    while ($file readdir($handle))    {
            
    //Check if the file is an exception
            
    if (in_array($q,$exceptions))    {
                continue;
            }
            
    //Check if it's showing '.' meaning this directory (the parent directory) or the directory above it (..)
            
    else if ($file == '.' || $file == '..')    {
                continue;
            }
            
    //Loops through all the extensions
            
    foreach ($exts as $extension)    {
                
    //Checks if it's an allowed file extension (eg. Word Documents, HTML Files etc.)
                
    if (strpos($file,$extension))    {
                    
    //Gets the file contents...
                    
    $c file_get_contents($dir.'\\'.$file);
                    
    //See if there's a match...
                    
    if (strpos($c,$q))    {
                        
    //Adds the URL
                        
    echo '<li><a href="'.$dir.''.$file.'" title="'.$file.'">'.getTitle($c,$file).'</a><br>';
                        
    //Gets the body content
                        
    echo getBody($c);
                        
    //Ends the list item
                        
    echo'</li>';
                        
    //Outputs it. This allows for the item to be displayed while the page is still loading. Good for when handling large files.
                        
    flush();
                    }
                }
                
    //Or else it'll continue through the loop.
                
    else    {
                    continue;
                }
            }
        }
        
    //And close the list.
        
    echo '</ol>';
    }
    //Tool: stripPHP($c)
    //Strips the PHP stuff for you so you can't search it.
    function stripPHP($c)    {
        if (
    strpos($c,'<?php') != FALSE)    {
            
    $php '';
            
    $php explode('<?php',$c);
            
    $php[1] = explode('?>',$php[1]);
            
    $c str_replace($php[1][0],'',$c);
            
    $c str_replace('<?php?>','',$c);
            return 
    $c;
        }
        else    {
            return 
    $c;
        }
    }
    //Tool: chopParagraph($str)
    //Chops the long content.
    function chopParagraph($str)    {
        
    $words 20;
        
    $chopped '';
        
    $str explode(' ',$str);
        if (
    count($str)>30)    {
            for (
    $i=0$i <= $words$i++)    {
                
    $chopped.=$str[$i].' ';
            }
            return 
    $chopped.'...';
        }
        else    {
            return 
    $str[count($str)-1];
        }
    }
    //Tool: getBody($content)
    //Gets the body content.
    function getBody($content)    {
        
    $content stripPHP($content);
        if (
    strpos($content,'<body>'))    {
            
    $content explode('<body>',$content);
            
    $content[1] = explode('</body>',$content[1]);
            
    $returner strip_tags($content[1][0]);
        }
        else    {
            
    $returner filterWeirdStuff($content); //For Word Documents...
        
    }
        return 
    chopParagraph($returner);
    }
    //Tool: getTitle($content,$filename)
    //Gets the title of the page. If there's no title, it'll display the page name.
    function getTitle($content,$filename)    {
        
    $content stripPHP($content);
        if (
    strpos($content,'<title>'))    {
            
    $content explode('<title>',$content);
            
    $content[1] = explode('</title>',$content[1]);
            return 
    htmlspecialchars($content[1][0]);
        }
        else    {
            return 
    $filename;
        }
    }
    //Tool: filterWeirdStuff($str)
    //Filters the ???????????????????***3%^#$^4 you see when trying to get the file contents of Word Documents. Ugh.
    function filterWeirdStuff($str)    {
        
    $str explode(' ',$str);
        unset(
    $str[0]);
        unset(
    $str[1]);
        
    $str implode(' ',$str);
        return 
    '...'.$str;
    }

    if (isset(
    $_POST['Submit'],$_POST['term'],$_GET['search']))    {
        
    searchfiles($_POST['term'], './');
    }
    else {
    ?>
    <form action="?search=true" method="post">
    <p>
      <label>Search: <input type="text" name="term"></label>
    </p>
    <p>
      <label><input type="submit" name="Submit" value="Search"></label>
    </p>
    </form>
    <?php
    }
    ?>
    Try check and see if it works for Acrobat Documents. I've got a tonne of homework to do, so I couldn't check it right now.
    ok i added that code. I have looked through it. Where do I show what directory that it should be looking in.

  8. #48
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    PHP Code:
        searchfiles($_POST['term'], './'); 
    Here. Change ./
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

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

    Default

    Quote Originally Posted by tech_support View Post
    PHP Code:
        searchfiles($_POST['term'], './'); 
    Here. Change ./
    Ok I changed that. Now here is what it is doing.
    It is giving me results (yippee we are getting somewhere) but here is what it is giving me:
    Warning: file_get_contents(ord\Microsoft Word - memorandum- recommended zoning ordinance.pdf): failed to open stream: No such file or directory in /home/content/d/e/r/derekm806/html/formtest.php on line 35
    Ok it is giving me this warning message but it looks like it is returning every file in the directory with this warning above just modified for each file.

    Here is the code that you gave me with my modifications:
    Dont know if it matters but I put this code in between the body tags of the document


    PHP Code:
    <?php
    function searchfiles($q,$dir)    {
        
    //Add the extensions you want to search through here.
        
    $exts = array('.php','.html','.htm','.docx','.doc','.pdf');
        
    //Enter the files that you DON'T want to search through, eg. this page.
        
    $exceptions = array('somefilethatyoudontwant',basename($_SERVER['PHP_SELF']));
        
    //Sets the beginning of the search results and outputs it.
        
    echo '<h2>Your Search Results</h2><ol>';
        
    flush();
        
    //Opens the directory
        
    $handle opendir($dir);
        
    //Starts a loop through the directory to list the files
        
    while ($file readdir($handle))    {
            
    //Check if the file is an exception
            
    if (in_array($q,$exceptions))    {
                continue;
            }
            
    //Check if it's showing '.' meaning this directory (the parent directory) or the directory above it (..)
            
    else if ($file == '.' || $file == '..')    {
                continue;
            }
            
    //Loops through all the extensions
            
    foreach ($exts as $extension)    {
                
    //Checks if it's an allowed file extension (eg. Word Documents, HTML Files etc.)
                
    if (strpos($file,$extension))    {
                    
    //Gets the file contents...
                    
    $c file_get_contents($dir.'\\'.$file);
                    
    //See if there's a match...
                    
    if (strpos($c,$q))    {
                        
    //Adds the URL
                        
    echo '<li><a href="'.$dir.''.$file.'" title="'.$file.'">'.getTitle($c,$file).'</a><br>';
                        
    //Gets the body content
                        
    echo getBody($c);
                        
    //Ends the list item
                        
    echo'</li>';
                        
    //Outputs it. This allows for the item to be displayed while the page is still loading. Good for when handling large files.
                        
    flush();
                    }
                }
                
    //Or else it'll continue through the loop.
                
    else    {
                    continue;
                }
            }
        }
        
    //And close the list.
        
    echo '</ol>';
    }
    //Tool: stripPHP($c)
    //Strips the PHP stuff for you so you can't search it.
    function stripPHP($c)    {
        if (
    strpos($c,'<?php') != FALSE)    {
            
    $php '';
            
    $php explode('<?php',$c);
            
    $php[1] = explode('?>',$php[1]);
            
    $c str_replace($php[1][0],'',$c);
            
    $c str_replace('<?php?>','',$c);
            return 
    $c;
        }
        else    {
            return 
    $c;
        }
    }
    //Tool: chopParagraph($str)
    //Chops the long content.
    function chopParagraph($str)    {
        
    $words 20;
        
    $chopped '';
        
    $str explode(' ',$str);
        if (
    count($str)>30)    {
            for (
    $i=0$i <= $words$i++)    {
                
    $chopped.=$str[$i].' ';
            }
            return 
    $chopped.'...';
        }
        else    {
            return 
    $str[count($str)-1];
        }
    }
    //Tool: getBody($content)
    //Gets the body content.
    function getBody($content)    {
        
    $content stripPHP($content);
        if (
    strpos($content,'<body>'))    {
            
    $content explode('<body>',$content);
            
    $content[1] = explode('</body>',$content[1]);
            
    $returner strip_tags($content[1][0]);
        }
        else    {
            
    $returner filterWeirdStuff($content); //For Word Documents...
        
    }
        return 
    chopParagraph($returner);
    }
    //Tool: getTitle($content,$filename)
    //Gets the title of the page. If there's no title, it'll display the page name.
    function getTitle($content,$filename)    {
        
    $content stripPHP($content);
        if (
    strpos($content,'<title>'))    {
            
    $content explode('<title>',$content);
            
    $content[1] = explode('</title>',$content[1]);
            return 
    htmlspecialchars($content[1][0]);
        }
        else    {
            return 
    $filename;
        }
    }
    //Tool: filterWeirdStuff($str)
    //Filters the ???????????????????***3%^#$^4 you see when trying to get the file contents of Word Documents. Ugh.
    function filterWeirdStuff($str)    {
        
    $str explode(' ',$str);
        unset(
    $str[0]);
        unset(
    $str[1]);
        
    $str implode(' ',$str);
        return 
    '...'.$str;
    }

    if (isset(
    $_POST['Submit'],$_POST['term'],$_GET['search']))    {
        
    searchfiles($_POST['term'], 'ord');
    }
    else {
    ?>
    <form action="?search=true" method="post">
    <p>
      <label>Search: <input type="text" name="term"></label>
    </p>
    <p>
      <label><input type="submit" name="Submit" value="Search"></label>
    </p>
    </form>
    <?php
    }
    ?>
    Last edited by tech_support; 07-27-2007 at 06:58 AM.

  10. #50
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Aha. It's because you have spaces in the file name.

    Change this line:
    PHP Code:
                    //Gets the file contents...
                    
    $c file_get_contents($dir.'\\'.$file); 
    to this:
    PHP Code:
                    //Gets the file contents...
                    
    $c file_get_contents($dir.'\\'.urlencode($file)); 
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

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
  •