Link to home
Start Free TrialLog in
Avatar of driven_13

asked on

Displaying files and folders in a specific directory on webpage

Hello all experts.

I have a folder which has a bunch of other folders and files in it.

I want to somehow show the contents of this folder on my webpage so that people can click on it and see the individual files and folders and download them.

A couple of my tech friends tell me that this can be done via php and Jquery of some sort.

Can someone please help me with this...???

I have attached a picture of what I am looking for more-or-less, does not have to exact.

Thanx in advance.

User generated image
Avatar of Element1910
Flag of United States of America image

You just need "Directory Listing" enabled on your web server for this feature. Then you can browse to your main folder that contains all the other folders (i.e.

If you have an apache server for your web server, then you can enable directory listing by creating or modifying your .htaccess file:

If you are using a Windows server for your webserver and have IIS and ASP.NET configured, you can enable directory listing by modifying your web.config and putting in this configuration:


<location path="mynewfolder">
    <directoryBrowse enabled="true" />


Open in new window

This is not really a question with an answer, it's a requirement for program development and for that you might want to hire a professional developer.  It will certainly get you a good result faster than trying to learn to be a professional developer, then writing the code.

But that aside, you can use the web site to find the documentation on how to do all of this.  Start with the intro here:

To read a directory you can use something like scandir().  To iterate over the results set, you can use foreach().  To display the names of the folders and directories, you can use echo.

In the documentation for the PHP functions, the user-contributed notes are often VERY helpful.  If you like technical books, this is an excellent starting place.

Best of luck with the project, ~Ray
Avatar of abolinhas
Flag of Portugal image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of driven_13


abolinhas, I went with option #1 (abeautifulsite) and it is exactly what I was looking for except for one thing.

I want the files when displayed in the tree to be clickable so that they can download them to their desktops.  At present the "a href" tag is referencing a hash-tag (line #4 below).

Can you (or someone else) please modify this code so that the files are actually clickable..??

		foreach( $files as $file ) {
			if( file_exists($root . $_POST['dir'] . $file) && $file != '.' && $file != '..' && !is_dir($root . $_POST['dir'] . $file) ) {
				$ext = preg_replace('/^.*\./', '', $file);
				echo "<li class=\"file ext_$ext\"><a href=\"#\" rel=\"" . htmlentities($_POST['dir'] . $file) . "\">" . htmlentities($file) . "</a></li>";

Open in new window

Thanx in advance,

Is the question, "How can I write an HTML anchor tag?"
To tell you the truth, I am not sure of my question.

All I want is that the files are clickable and I am not sure how to reference the directory structure to point to the files correctly.

Ok, I have figured out how to reference the file itself but when I click on it, nothing happens.  Usually it should pop up a window that says "View/Save" but it does not do this.

Below is where the changed code that is actually referencing the file but not downloading when clicked:

		foreach( $files as $file ) {
			if( file_exists($root . $_POST['dir'] . $file) && $file != '.' && $file != '..' && !is_dir($root . $_POST['dir'] . $file) ) {
				$ext = preg_replace('/^.*\./', '', $file);
				echo "<li class=\"file ext_$ext\"><a href=\"" . htmlentities($_POST['dir'] . $file) . "\" rel=\"" . htmlentities($_POST['dir'] . $file) . "\">" . htmlentities($file) . "</a></li>";

Open in new window

Any help to fix this will be greatly appreciated.

Another Update:

I have also figured out to make those files clickable and download-able with one last little snag.

I cannot force the "Save As..." window to pop up.  If the browser realizes that it is a file type it can handle (e.g. jpg, gif, php, html, etc.) then it actually displays them without showing that "Save As..." pop-up window.

Here is the code I changed to actually let it download:

<script type="text/javascript">
   function openFile(file) {
   window.location = file;

$(document).ready( function() {
	$('#fileTreeDemo_1').fileTree({ root: '../', script: 'jqueryFileTree.php' }, function(file) { 

Open in new window

The "openFile" function has to be tweaked somehow to make this happen.

Any ideas.....????

Thanx as always.

Given enough time I can figure anything out.