The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Populate list with directory contents

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
  • #148230

    I want to populate a list with the contents of a folder. The folder has 100’s of PDF’s. I would rather let it dynamically populate the list based on it’s contents instead of having to update the list manually. Can this be done?

    Page Link : (non-functional links) File Directory : (non-functional links)

    I’m not that well versed in jquery to know if it can be done.


    You won’t be able to do this with just javascript, it will not allow you to query a folder. However you could get it working with a fairly simple php script that returns a json object that you can parse with javascript.

    $files = array();
    $dir = opendir('.');
    while(false != ($file = readdir($dir))) {
            if(($file != ".") and ($file != "..") and ($file != "directory.php")) {
    //make sure the file isn't directory or this file
                    $files[] = $file; // put in array.
    natsort($files); // sort.
    $end = end($files);
    // print.
    foreach($files as $file) {
        if ($end !== $file){
            echo("{\"file\": \"$file\" },");
            echo("{\"file\": \"$file\" }");

    note that “directory.php” will be whatever you name this file.

    If you save that, and drop it in to the folder you have your files in you should be able to do an ajax call with JQuery.

    ie: $.getJSON('directory/directory.php', function(data){ console.log(data); });


    @Willson – Thanks for that but unfortunately the IT director where I work doesn’t allow PHP scripts. Hence why I was hoping I could do it with just Javascript.


    Ah, I was afraid of that. Hopefully you can find a solution.

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.