15

Is there any way to list the files of a directory in a static webpage with the link to view the file?

I would like to upload some PDF files to a certain directory of my static website (it uses HTML and JS), and want to show the list of the files in a page with the link to view the pdf files. That way, if I upload new files, I don't have to modify the HTML page every time. Is there any way to do that?

2540625
  • 11,022
  • 8
  • 52
  • 58
B.M.
  • 318
  • 2
  • 3
  • 19
  • The linux 'tree' command can generate a HTML directory listing... `tree -H '.' -T "Listing" -L 1 --noreport --charset utf-8 > listing.html` – anthony Oct 05 '22 at 23:16

8 Answers8

7

If you are using Apache as a web-server and have configured mod-autoindex for the directory you upload pdf files then you should probalby see somethink like this when navigation to that url:

enter image description here

This auto-generated page can be easily parsed by js using jquery:

var pdfFilesDirectory = '/uploads/pdf/';

// get auto-generated page 
$.ajax({url: pdfFilesDirectory}).then(function(html) {
    // create temporary DOM element
    var document = $(html);

    // find all links ending with .pdf 
    document.find('a[href$=.pdf]').each(function() {
        var pdfName = $(this).text();
        var pdfUrl = $(this).attr('href');

        // do what you want here 
    })
});
Community
  • 1
  • 1
Olim Saidov
  • 2,796
  • 1
  • 25
  • 32
  • 1
    works for me only if I have quotes in the attribute selector ``document.find('a[href$=".pdf"]).each(...)``. – jerik Jun 07 '17 at 15:21
  • The Apache directory index system can do a LOT more than just a basic index. It can add headers, footers, which in turn can color the pages. The icons can be changed, files ignored by the listing, and you can even add a test decription to be added to each line. An example *advanced* fancy apache directory listing can be seen at... https://antofthy.gitlab.io/info/perl/ This was generated by appache, but then uploaded to gitlab which only serves static web pages. – anthony Oct 05 '22 at 23:28
3

You need to have a server-side implementation, you could do this by using PHP for example. You cannot do this with JavaScript, because it is run on the client-side, and cannot access the files on the server.

meskobalazs
  • 15,741
  • 2
  • 40
  • 63
2

I made a node module to automate the task of getting all files and folders: mddir

Usage

node mddir "../relative/path/"

To install: npm install mddir -g

To generate markdown for current directory: mddir

To generate for any absolute path: mddir /absolute/path

To generate for a relative path: mddir ~/Documents/whatever.

The md file gets generated in your working directory.

Currently ignores node_modules, and .git folders.

Troubleshooting

If you receive the error 'node\r: No such file or directory', the issue is that your operating system uses different line endings and mddir can't parse them without you explicitly setting the line ending style to Unix. This usually affects Windows, but also some versions of Linux. Setting line endings to Unix style has to be performed within the mddir npm global bin folder.

Line endings fix

Get npm bin folder path with:

npm config get prefix

Cd into that folder

brew install dos2unix

dos2unix lib/node_modules/mddir/src/mddir.js

This converts line endings to Unix instead of Dos

Then run as normal with: node mddir "../relative/path/".

I made another node module call agd, to generate a tree view based on the other module: https://github.com/JohnByrneRepo/agd.

Auto generated documentation (Alpha)

Functionality so far:

Generates a tree folder structure in node, that is rendered as a treegrid in the browser. Click on a file (non-root level) to populate main view.

Coming soon:

Generates a documentation guide including function names and parameters, function dependencies, and more. Initially compatible with jQuery and plain JavaScript function namespacing, soon to be compatible with React, Redux, Angular 1, Angular 2 and other frameworks on request.

Usage

node agd relativePath

e.g. node agd '../../'

Generated code.json.

Run 'node http-server' then open the browser to view the file structure rendered in the sidebar. Larger projects can take up to a minute or two to render.

See code.json for example generated data.

To-do: Add code content for top level files. Move tree html generation into node.

Contact html5css3@outlook.com

MIT License

Example generated tree structure

enter image description here

John Byrne
  • 569
  • 5
  • 6
1
  1. Open a browser
  2. Navigate to the folder you want listed
  3. If you see a list of the files, continue
  4. Make an ajax call to that folder (example below)
  5. response will be the html of the listing page
  6. You can parse that out to get the file listing

Example:

// This is in angular, but you can use whatever
$http.get('folder_to_list/').success(function(response) {
    console.log(response);
});
Community
  • 1
  • 1
Travis Heeter
  • 13,002
  • 13
  • 87
  • 129
  • keep in mind it won't work for local files, I mean if you're running from disk not a server, because ajax calls are blocked for security reasons (like this very one reason -- not to see internal files) – Marcelo Ruggeri Apr 26 '17 at 11:03
0

You need a combination of javascript and PHP. You could call the PHP file through Javascript, by using an AJAX call. try this PHP file which should return an Json object:

$directory = "/directory/path";
$pdffiles = glob($directory . "*.pdf");

$files = array();

foreach($pdffiles as $pdffile)
{
   $files[] = "<a href=$pdffile>".basename($pdffile)."</a>";
}

echo json_encode($files);

Now you just need to loop through the Json object to list the Url's.

Something like:

$.getJSON( "file.php", function( data ) {
  var items = [];
  $.each( data, function(val ) {
    items.push(val);
  });

  $( "body" ).append( items );
});

Did not test it, but something like this should work.

Erik van de Ven
  • 4,747
  • 6
  • 38
  • 80
0

Instead of JavaScript, which runs only on the client side, you should consider using PHP or other server language, to crawl your directory of files and list them inside an HTML file/template. PHP for example has scandir function, which can list files in a dicrectory

Kristian Ivanov
  • 111
  • 1
  • 9
  • Thanks for your reply, but sad to say that i dont have php services at the server. – B.M. Jan 09 '15 at 12:32
  • 1
    You can't use client-side scripting to list serverside things for a lot of security reasons. What is the server running on? It is similar on all server side languages. – Kristian Ivanov Jan 09 '15 at 12:35
  • 1
    You can actually make an ajax call to the directory of those pdf files, parse the response and generate your links. It is however an extremely ugly solution – Kristian Ivanov Jan 09 '15 at 12:41
0

Be simple. Put all your files in a directory and don't make a homepage of that directory. Then, in the page you want, add an Iframe that shows that directory. Then you will see the list of files you uploaded, all in hyperlinks. When you click on the links, the Iframe will show the PDF files.

Ivan Law
  • 31
  • 5
0

I have the same problem.

I used to use an apache webserver with its 'fancy directory listings' and had everything setup that way, complete with headers, footers, color schemes, etc.

Then I migrated to gitlab webservers which is pure static pages only. NO Directory listings. Arrggghh...

My solution...

I continue to have the pages served in a local apache server (not world accessable), then I download the "index.html" file it generates, before uploading the index page to gitlab.

Example page generated from apache fancy directory listing... https://antofthy.gitlab.io/info/www/

I do the same for a set of pages that used Server-Side Includes (shtml), having apache expand the page to static HTML.

Example apache SSI generated page... https://antofthy.gitlab.io/graphics/polyhedra/

Of course this does not work with pages that rely on executable output, or CGI scripts, but for directory listings it is just fine.

Of course I would prefer to find a SSG that knows apache fancy directory listing, SSI, or even basic directory listings, that isn't over kill. BUt that is an on going search.

anthony
  • 7,696
  • 1
  • 17
  • 11