+ Post New Thread
Results 1 to 12 of 12
Web Development Thread, Listing Contents of a folder in Coding and Web Development; Hiya, does anybody know a way to list the contents of a web directory folder. for example i have a ...
  1. #1

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,714
    Thank Post
    269
    Thanked 1,116 Times in 1,012 Posts
    Rep Power
    345

    Listing Contents of a folder

    Hiya,

    does anybody know a way to list the contents of a web directory folder.

    for example i have a folder which has 4 pdf files in, with one more added each month. for this i would like a webpage front end which lets you choose which month you would like to view.

    e.g. i have october 09.pdf november 09.pdf december 09.pdf january 10.pdf and i would like to have a webpage that lists the contents of the folder automatically so i don't need to add each page in manually

    Toby

  2. #2

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,182
    Thank Post
    1,285
    Thanked 1,029 Times in 729 Posts
    Rep Power
    658
    Depends how much you want to customise it. For a simple list, just the name of the directory typed into the address bar will do it

  3. #3

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    240
    Can you use PHP scripts on the web server? If so, the following should get you there:

    PHP Code:
    <?php
    function dirList ($directory
    {

        
    // create an array to hold directory list
        
    $results = array();

        
    // create a handler for the directory
        
    $handler opendir($directory);

        
    // keep going until all files in directory have been read
        
    while ($file readdir($handler)) {

            
    // if $file isn't this directory or its parent, 
            // add it to the results array
            
    if ($file != '.' && $file != '..')
                
    $results[] = $file;
        }

        
    // tidy up: close the handler
        
    closedir($handler);

        
    // done!
        
    return $results;
    }

    //Call the function above
    $results dirList('PATH TO DIR');

    //Print results
    foreach($results as $item){
        echo 
    "$item<br />";
    }
    Source: Jon Haworth - PHP: List files in a directory (tweaked to give you an example)

    If you want to add a link to each file simply change the foreach to:

    PHP Code:
    foreach($results as $item){
        echo 
    '<a href="DIRPATH/' $item '">' $item '</a><br />'
    All this code should work, but I haven't tested it.

  4. #4

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    240
    Quote Originally Posted by JoshJohnson View Post
    Depends how much you want to customise it. For a simple list, just the name of the directory typed into the address bar will do it
    Most web servers have directory listing disabled, and it's good practice to do this. Therefore in most setups this won't work and will instead produce a "Directory Listing not allowed" error.

  5. #5

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,714
    Thank Post
    269
    Thanked 1,116 Times in 1,012 Posts
    Rep Power
    345
    i will give the php script a go, i think php scripting in enabled. i will post how i get on

    Toby

  6. #6

    Join Date
    Nov 2009
    Posts
    3
    Thank Post
    1
    Thanked 1 Time in 1 Post
    Rep Power
    0
    If it's a public facing site you really don't want that script to work, to be honest. Even if it's for an intranet probably not

  7. #7

    SimpleSi's Avatar
    Join Date
    Jun 2005
    Location
    Lancashire
    Posts
    5,720
    Thank Post
    1,451
    Thanked 580 Times in 434 Posts
    Rep Power
    165
    Quote Originally Posted by natparnell View Post
    If it's a public facing site you really don't want that script to work, to be honest. Even if it's for an intranet probably not
    Why not?

    regards

    Simon

  8. #8

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    17,095
    Thank Post
    511
    Thanked 2,309 Times in 1,785 Posts
    Blog Entries
    24
    Rep Power
    803
    Quote Originally Posted by natparnell View Post
    If it's a public facing site you really don't want that script to work, to be honest. Even if it's for an intranet probably not
    I can't see anything which could cause an issue. It would be safer than turning on directory listing (as that could accidentally lead to directories you don't want to be exposed, being exposed).

  9. #9

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    240
    Quote Originally Posted by natparnell View Post
    If it's a public facing site you really don't want that script to work, to be honest. Even if it's for an intranet probably not
    There's nothing the matter with that script. The only security hole is that it shows files in a directory (and in the latter one provides a link to the file), but that is what the OP wants to do.

  10. #10

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,714
    Thank Post
    269
    Thanked 1,116 Times in 1,012 Posts
    Rep Power
    345
    Sorry to drag this up - have just got round to fully testing my website!

    I have the following code - Edited from the above I have succesfully made it list the contents of the folder (/documents/newsletters but when you click the hyperlink it is looking in the root of the website not the folder that i am after - which as you can imagine is rather annoying! it might just be me as its late on a bank holiday but i can't see where im going wrong!
    PHP Code:
    <?php
     
    $myDirectory 
    opendir("./documents/newsletters/");


    while(
    $entryName readdir($myDirectory)) {
        
    $dirArray[] = $entryName;
    }


    closedir($myDirectory);


    $indexCount    count($dirArray);



    sort($dirArray);


    print(
    "<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n");
    print(
    "<TR><TH>Issue</TH></TR>\n");

    for(
    $index=0$index $indexCount$index++) {
            if (
    substr("$dirArray[$index]"01) != "."){ 
            print(
    "<TR><TD><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>");
            print(
    "</TR>\n");
        }
    }
    print(
    "</TABLE>\n");
    ?>
    Any help appreciated! now time for sleep!

    Toby

  11. #11

    Join Date
    May 2009
    Posts
    11
    Thank Post
    0
    Thanked 5 Times in 5 Posts
    Rep Power
    10

    Re: Listing Contents of a folder

    You should just need to put the folder "documents/newsletters/" in the link and it will work.

    So:

    PHP Code:
    print("<TR><TD><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>"); 
    Would become:

    PHP Code:
    print("<TR><TD><a href=\"documents/newsletters/$dirArray[$index]\">$dirArray[$index]</a></td>"); 
    I have rewritten the code below to use the PHP SPL library to access the files if your interested.

    PHP Code:
    <?php
    $directoryPath 
    'documents/newsletters';

    $linksArray = array();
    $filesArray = new DirectoryIterator($directoryPath);

    foreach (
    $filesArray as $file) {
        if(
    $file->isDot()) {
            continue;
        }

        
    $linksArray[] = $file->getFilename();
    }

    if (
    $linksCount count($linksArray)) {
        echo 
    '<table border="1" cellpadding="5" cellspacing="0" class="whitelinks"><tr><th>Issue</th></tr>';

        foreach (
    $linksArray as $link) {
            echo 
    '<tr><td><a href="' $directoryPath '/' $link '">' $link '</a></td></tr>';
        }

        echo 
    '</table>';
    }

  12. Thanks to leeneilson from:

    glennda (5th April 2010)

  13. #12

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,714
    Thank Post
    269
    Thanked 1,116 Times in 1,012 Posts
    Rep Power
    345
    Cheers lee, i knew it was me just not seeing the obvious!

    thanks

    Toby

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 5
    Last Post: 29th January 2013, 10:16 AM
  2. Empty the contents of a folder
    By FN-GM in forum Scripts
    Replies: 13
    Last Post: 12th March 2008, 11:44 PM
  3. Replies: 1
    Last Post: 21st January 2007, 02:51 PM
  4. VB Script ti check for folder and then copy contents to PC
    By tosca925 in forum How do you do....it?
    Replies: 4
    Last Post: 2nd October 2006, 08:15 PM
  5. Replies: 2
    Last Post: 1st October 2005, 02:55 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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