- This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
Viewing 4 posts - 1 through 4 (of 4 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Home › Forums › JavaScript › Populate list with directory contents
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 : http://www.mysite.com/newsletters (non-functional links) File Directory : http://www.mysite.com/assets/documents/newsletters/ (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.
<?php
$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.
echo("[");
foreach($files as $file) {
if ($end !== $file){
echo("{\"file\": \"$file\" },");
}else{
echo("{\"file\": \"$file\" }");
}
}
echo("]");
?>
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.