Skip to content

Instantly share code, notes, and snippets.

@abalter
Last active December 5, 2023 22:42
Show Gist options
  • Select an option

  • Save abalter/b5357657311349e06bc5b32222f37030 to your computer and use it in GitHub Desktop.

Select an option

Save abalter/b5357657311349e06bc5b32222f37030 to your computer and use it in GitHub Desktop.
Get directory listing on server using JavaScript
var url = "http://someserver/somepath/somedir/?C=M;O=A";
$.get(url, (data) =>
{
console.log(data);
let listing = parseDirectoryListing(data);
$('body').append(JSON.stringify(listing));
});
function parseDirectoryListing(text)
{
let docs = text
.match(/href="([\w]+)/g) // pull out the hrefs
.map((x) => x.replace('href="', '')); // clean up
console.log(docs);
return docs;
}

Access Directory Listing from Client Side

This code can list the contents of a directory on the server that is serving a webpage running the code.

Requirement

In order for this to work, Apache needs to be running mod-autoindex.

http://httpd.apache.org/docs/2.2/mod/mod_autoindex.xml

Sorting and Formatting

Apache autoindexing allows basic sorting and formating by appending a query string.

For instance, http://someserver/somepath/somedir/?C=M;O=A will sort the listing by modification date in ascending order.

  • C=N sorts the directory by file name
  • C=M sorts the directory by last-modified date, then file name
  • C=S sorts the directory by size, then file name
  • C=D sorts the directory by description, then file name
  • O=A sorts the listing in Ascending Order
  • O=D sorts the listing in Descending Order
  • F=0 formats the listing as a simple list (not FancyIndexed)
  • F=1 formats the listing as a FancyIndexed list
  • F=2 formats the listing as an HTMLTable FancyIndexed list
  • V=0 disables version sorting
  • V=1 enables version sorting
  • P=pattern lists only files matching the given pattern
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment