+ 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
    The Office
    Posts
    5,434
    Thank Post
    236
    Thanked 696 Times in 629 Posts
    Rep Power
    226

    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. IDG Tech News

  3. #2

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    2,861
    Thank Post
    756
    Thanked 482 Times in 355 Posts
    Rep Power
    266
    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

  4. #3

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    493
    Thanked 688 Times in 443 Posts
    Rep Power
    237
    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.

  5. #4

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    493
    Thanked 688 Times in 443 Posts
    Rep Power
    237
    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.

  6. #5

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    The Office
    Posts
    5,434
    Thank Post
    236
    Thanked 696 Times in 629 Posts
    Rep Power
    226
    i will give the php script a go, i think php scripting in enabled. i will post how i get on

    Toby

  7. #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

  8. #7

    SimpleSi's Avatar
    Join Date
    Jun 2005
    Location
    Lancashire
    Posts
    4,965
    Thank Post
    1,188
    Thanked 445 Times in 348 Posts
    Rep Power
    136
    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

  9. #8

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Bristol
    Posts
    12,970
    Blog Entries
    24
    Thank Post
    438
    Thanked 1,502 Times in 1,179 Posts
    Rep Power
    459
    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).

  10. #9

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    493
    Thanked 688 Times in 443 Posts
    Rep Power
    237
    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.

  11. #10

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    The Office
    Posts
    5,434
    Thank Post
    236
    Thanked 696 Times in 629 Posts
    Rep Power
    226
    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

  12. #11
    leeneilson's Avatar
    Join Date
    May 2009
    Location
    Telford
    Posts
    11
    Thank Post
    0
    Thanked 5 Times in 5 Posts
    Rep Power
    7

    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>';
    }

  13. Thanks to leeneilson from:

    glennda (5th April 2010)

  14. #12

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    The Office
    Posts
    5,434
    Thank Post
    236
    Thanked 696 Times in 629 Posts
    Rep Power
    226
    Cheers lee, i knew it was me just not seeing the obvious!

    thanks

    Toby

SHARE:
+ Post New Thread

Similar Threads

  1. Empty the contents of a folder
    By FN-GM in forum Scripts
    Replies: 13
    Last Post: 13th March 2008, 12:44 AM
  2. Replies: 4
    Last Post: 22nd January 2007, 08:48 PM
  3. Replies: 1
    Last Post: 21st January 2007, 03: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
  •