Discover Habbo's history
Treat yourself with a Secret Santa gift.... of a random Wiki page for you to start exploring Habbo's history!
Happy holidays!
Celebrate with us at Habbox on the hotel, on our Forum and right here!
Join Habbox!
One of us! One of us! Click here to see the roles you could take as part of the Habbox community!


Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2006
    Location
    London
    Posts
    3,536
    Tokens
    170

    Latest Awards:

    Lightbulb [TUT] Displaying files of a directory & search function!

    Hey guys,

    Firstly, before I start with the code - I haven't been in these forums for quite a while and so do not know what everyone is like now however this is a script I wish to share with everyone. It may not be teaching them what each step is (because I know some of you will be saying, "This isn't a TUT!") but it is helping people with something I wanted to do for ages. The scripts are in quotes because I needed to highlight text in red - which the code tags did annoyingly.

    Let's say for example you have about 1000 voice clips in a directory and you do not wish to make a document, and make links to each and every one of the files. You want to simply let people view all the files with the desired style instead of leaving it as the root directory with the "Parent Directory" link visible and the "Apache" text below. To do this, you need to add the following script to your page:

    <?php
    $files = array();

    if ($handle = opendir('DIRECTORY')) {

    while (false !== ($file = readdir($handle)))
    { if ($file != "." && $file != "..")
    { $files[count($files)] = $file;
    }
    }
    sort($files);

    foreach ($files as $file) {
    echo "<a href='DIRECTORY/$file'>$file</a><br>";
    }
    closedir($handle);
    }
    ?>
    You only need to replace the "DIRECTORY"s with the name of your directory (assuming this file is parent to it).

    Now you have done this, and got all the voice clips (the files in this example), you want people to be able to search for a file. It will take ages for them to go through the whole list, so you set up a search function!

    Add the code below to the part of the page you wish to make search the directory. Replace everything in red!

    <form action="FILEnameTHISformISon.php" method="get">
    <b>Search For A VOICECLIP:</b><br>
    <input type="text" name="search" /><br>
    <input type="submit" value="Find VOICECLIP" name="submit"/>
    </form>

    <?php
    $files = array();
    $searchtext = $_REQUEST['search'];
    if ($searchtext=="") { echo "ERROR MESSAGE FOR NOTHING BEING ENTERED!";
    } else {

    if ($handle = opendir('DIRECTORY')) {
    $found = false;
    while (false !== ($file = readdir($handle)))
    { if (stristr($file, $searchtext))

    { $files[count($files)] = $file;
    }
    }
    sort($files);

    foreach ($files as $file) {
    echo "<a href='DIRECTORY/$file'>$file</a><br>";
    $found = true;
    }
    if (!$found) { echo "ERROR MESSAGE FOR THERE BEING NO RESULTS ON THE SEARCH!"; }
    }
    closedir($handle);
    }
    ?>
    I hope this has helped some people.

    Thanks,
    Danny
    Last edited by H0BJ0B; 04-10-2008 at 12:08 AM.
    2005: JOINED ; Radio DJ

    2006: Radio DJ ; Senior DJ

    2007: HxTV Flash Artist ; Productions Staff ; HxHD Staff ; Head DJ ; Events Organiser ; Productions Staff ; Competitions Staff ; Assistant Radio Manager

    2008: Senior Competitions Staff ; Forum Moderator ; HxHD Staff ; Competitions Manager ; Graphics Designer

    2009: LEFT ; Guest DJ

  2. #2
    Join Date
    Jan 2008
    Posts
    3,711
    Tokens
    100

    Latest Awards:

    Default

    I don't know PHP, but I guess it's a good TUT

  3. #3
    Join Date
    Jul 2007
    Location
    UK
    Posts
    2,470
    Tokens
    2,975

    Latest Awards:

    Default

    Good TUT +Rep ive been looking for something like this for ages!!!

  4. #4
    Join Date
    Apr 2008
    Location
    Derby
    Posts
    4,668
    Tokens
    262

    Latest Awards:

    Default

    Rerli nice =] Thanks for posting.
    Back for a while

  5. #5
    Join Date
    Mar 2008
    Posts
    5,108
    Tokens
    3,780

    Latest Awards:

    Default

    Cleaned it up a little bit, made it all into one page, and made the variables all at the top.

    PHP Code:
    <?php
    /*
     * Edit the 'Configuration Variables' below.
     */
    $directory "bla"// Please leave slashes out.
    $errorMessage "There were no results found.";

    /*
     * Do not modify the code below.
     */

    $action $_GET ["action"];
    $files = array ();
    ?>
    <style type="text/css">
    body {
        font-family: Arial;
        font-size: 10pt;
    }
    </style>

    <h3>Directory Listing For: <?php
    echo $directory;
    ?></h3>

    <?php
    if($action == "search") {
        
    $searchText $_POST ["search"];
        if(
    $handle opendir'bla' )) {
            
    $found false;
            while( 
    false !== ($file readdir$handle )) ) {
                if(
    stristr$file$searchText )) 

                {
                    
    $files [count$files )] = $file;
                }
            }
            
    sort$files );
            
            foreach( 
    $files as $file ) {
                echo 
    "<a href=\"{$directory}/{$file}\">{$file}</a><br>";
                
    $found true;
            }
            if(! 
    $found) {
                echo 
    $errorMEssage;
            }
        }
        
    closedir$handle );

    } else {
        
        if(
    $handle opendir$directory )) {
            
            while( 
    false !== ($file readdir$handle )) ) {
                if(
    $file != "." && $file != "..") {
                    
    $files [count$files )] = $file;
                }
            }
            
    sort$files );
            
            foreach( 
    $files as $file ) {
                echo 
    "<a href=\"{$directory}/{$file}\">{$file}</a><br>";
            }
            
    closedir$handle );
        }
    }
    ?>

    <br />
    <hr size="1" color="#969696" />
    <br />

    <form action="?action=search" method="post"><b>Search files:</b><br>
    <input type="text" name="search" /><br />
    <br />
    <input type="submit" value="Find File" name="submit" /></form>
    Last edited by Dentafrice; 04-10-2008 at 06:09 PM.

  6. #6
    Join Date
    Feb 2007
    Location
    Essex, England
    Posts
    1,392
    Tokens
    0

    Latest Awards:

    Default

    Ty for this.
    Ty also to dentafrice.

    +rep


  7. #7
    Join Date
    Dec 2006
    Location
    London
    Posts
    3,536
    Tokens
    170

    Latest Awards:

    Default

    You're welcome!
    2005: JOINED ; Radio DJ

    2006: Radio DJ ; Senior DJ

    2007: HxTV Flash Artist ; Productions Staff ; HxHD Staff ; Head DJ ; Events Organiser ; Productions Staff ; Competitions Staff ; Assistant Radio Manager

    2008: Senior Competitions Staff ; Forum Moderator ; HxHD Staff ; Competitions Manager ; Graphics Designer

    2009: LEFT ; Guest DJ

Posting Permissions

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