Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Script to search for local files.

    Hi,

    I need help to make a script that checks files in a local folder and then letting a user search for a part of the filename. If the file that the user searches for exists it should output a download link.
    This is my code:

    <?PHP
    $dir = "files/";
    $handle = opendir($dir);
    while ($folder = readdir($handle))
    {
    $folders[] = $folder;
    }
    closedir($handle);
    foreach ($folders as $folder) {
    echo "<a href=$dir$folder>$folder</a>"."<br />";
    }
    ?>

    I have a search for like this:
    <form action="" method="post" id="formen">
    <input id="find" type="text" name="find" size="30" />
    <button name="submit" type="submit" value="Skicka" tabindex="6">find</button>
    </form>

    How can i let the users get a download link only if the thing they are searching for exists?

    Please help me.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Hi, in the future could you please wrap your code in [php][/php] tags to preserve the format (I know its kinda short here, but longer ones are impossible to read without).
    The easiest way to do what you are looking to do, which is a scalar search for a file in a single directory is to use the glob() function. It takes a simple pattern and finds results:
    PHP Code:
    $toFind htmlspecialchars($_POST['find']);
    $dir "files/";
    foreach (
    glob($dir $toFind) AS $found)
    {
        echo 
    $found// Throw your links in here.

    Given the simplistic parsing of glob, you can actually get relatively complex with them without using preg.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    torrjuck (11-26-2010)

  • #3
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you very much!
    But now im am forced to enter the exact filename in the search-form. Is it possible to only enter a part of it?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Yes, you need to wildcard it. This method really only works if you leave the extension off, but if you want to allow it you can use parse_url to remove the extension bit, or even use it to your advantage.
    PHP Code:
    $toFind "*" htmlspecialchars($_POST['find']) . "*"
    That wildcard should let you search for 'find' anywhere in the middle. Like I said, no extension though on this one (see the parse_url on php.net to remove the extension if you like. Basename is another option).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    torrjuck (11-26-2010)

  • #5
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you, but something is still wrong. Its not returns all search-results.
    Can you please take one more look?

    PHP Code:
    $dir "files/";
    $handle opendir($dir);
    while (
    $folder readdir($handle))
    {
    $folders[] = strtolower($folder);
    }
    closedir($handle);
    foreach (
    $folders as $folder) { 
    //echo "<a href=$dir$folder>$folder</a>"."<br />"; 
    }


    if(isset(
    $_POST["find"])) {
    $sokdata strtolower($_POST["find"]);
    $toFind "*" htmlspecialchars($sokdata) . "*";
    $dir "files/";
    $found strtolower($found);
    foreach (
    glob($dir $toFind) as $found)
    {
        echo 
    "<a href=$found>$found</a>"."<br />";
    }  


  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by torrjuck View Post
    Thank you, but something is still wrong. Its not returns all search-results.
    Can you please take one more look?

    PHP Code:
    $dir "files/";
    $handle opendir($dir);
    while (
    $folder readdir($handle))
    {
    $folders[] = strtolower($folder);
    }
    closedir($handle);
    foreach (
    $folders as $folder) { 
    //echo "<a href=$dir$folder>$folder</a>"."<br />"; 
    }


    if(isset(
    $_POST["find"])) {
    $sokdata strtolower($_POST["find"]);
    $toFind "*" htmlspecialchars($sokdata) . "*";
    $dir "files/";
    $found strtolower($found);
    foreach (
    glob($dir $toFind) as $found)
    {
        echo 
    "<a href=$found>$found</a>"."<br />";
    }  

    I just upgraded to Windows 7 and haven't installed PHP or anything yet. Does it find any results at all, or just no results? If you print the $sokdata, what is in it?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Its only echo the letters that i entered in the "find"-form, but in lowercase.
    I think it's something wrong with the lowercase-commandos somewere. If the files only contains lowercase letters it will find the file, but if it contains uppercase letters its harder to enter something in the form to make it find the file. If the filename is ABcDE i need to enter "c" or "C" in the find-form. It seems that the variable $found doesnt transforms to lowercases. What is wrong?

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Nothing wrong with that I'm afraid. *nix systems are case sensitive, so that is correct.
    If you check the glob api: http://php.ca/manual/en/function.glob.php, the first user comment there is actually a block of code to write a function called globistr, which is a case-insensitive look.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #9
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Nothing wrong with that I'm afraid. *nix systems are case sensitive, so that is correct.
    If you check the glob api: http://php.ca/manual/en/function.glob.php, the first user comment there is actually a block of code to write a function called globistr, which is a case-insensitive look.
    I do not fully understand. Can you please write a little bit code so i understand?
    Thank you very muck for all the help!

  • #10
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    You could always go the less efficient route:

    PHP Code:
    $folder scandir('folder');
    foreach(
    $folder as $file) {
      
    $file basename($file); // basic sanitisation
      
    if (is_file($folder.'/'.$file) and (stripos($file$_POST["find"]) !== false)) {
        echo(
    '<a href="'.$folder.'/'.$file.'">Download</a>');

    untested.

    stripos does a case insensitive search in the filename
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog


  •  

    Posting Permissions

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