Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a simple index.html file which lists all files/directories?

Tags:

html

webserver

We use a web server that does not allow directory listing.

There is a specific directory I would like to allow listing of.

How can make a simple HTML file that will contain the contents of this directory?

like image 331
David B Avatar asked Sep 24 '10 07:09

David B


People also ask

How do you list directories in html?

Use the dir tag in HTML to display directory list. This is very similar to <ul> tag but do not use <dir> since it is deprecated now.

How do I create an index for all files in a folder?

To create an index fileOn the File menu, click New, and then click Index. Add keywords to the index (. hhk) file you have created. If you plan to use your index only on a Web site, you can create a site map index.


2 Answers

There are enough valid reasons to explicitly disable automatic directory indexes in apache or other web servers. Or, for example, you might want to only include certain file types in the index. In such cases you might still want to have a statically generated index.html file for specific folders.

tree

tree is a minimalistic utility that is available on most unix-like systems (ubuntu/debian: sudo apt install tree, mac: brew install tree, windows: zip). tree can generate plain text, XML, JSON or HTML output.

Generate an HTML directory index one level deep:

tree -H '.' -L 1 --noreport --charset utf-8 -o index.html 

Only include specific file types that match a glob pattern, e.g. *.zip files:

tree -H '.' -L 1 --noreport --charset utf-8 -P "*.zip" -o index.html 

The argument to -H is what will be used as a base href, so you can pass either a relative path such as . or an absolute path from the web root, such as /files. -L 1 limits the listing to the current directory only.

See tree --help or man tree in your terminal for all the supported options.

Generator script with recursive traversal

I needed an index generator which I could style the way I want, and which would also include the file sizes, so ended up writing this script (python 3) which in addition to having customisable styling can also recursively generate an index.html file in all the nested subdirectories (with the --recursive or -r flag). The styling borrows heavily from caddyserver's file-server module. It includes last modified times and is responsive in mobile viewports.

like image 125
ccpizza Avatar answered Oct 06 '22 12:10

ccpizza


For me PHP is the easiest way to do it:

<?php echo "Here are our files"; $path = "."; $dh = opendir($path); $i=1; while (($file = readdir($dh)) !== false) {     if($file != "." && $file != ".." && $file != "index.php" && $file != ".htaccess" && $file != "error_log" && $file != "cgi-bin") {         echo "<a href='$path/$file'>$file</a><br /><br />";         $i++;     } } closedir($dh); ?>  

Place this in your directory and set where you want it to search on the $path. The first if statement will hide your php file and .htaccess and the error log. It will then display the output with a link. This is very simple code and easy to edit.

like image 30
ryryan Avatar answered Oct 06 '22 11:10

ryryan