Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Displaying files and folders in a specific directory on webpage

Posted on 2012-12-28
10
Medium Priority
?
218 Views
Last Modified: 2012-12-29
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.

--d.
Rough idea of what I want it to look like.
0
Comment
Question by:driven_13
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 7

Expert Comment

by:Element1910
ID: 38727152
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. www.notarealsite.com/Images/).

If you have an apache server for your web server, then you can enable directory listing by creating or modifying your .htaccess file: http://www.clockwatchers.com/htaccess_dir.html

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:

<configuration>

<location path="mynewfolder">
   <system.webServer>
    <directoryBrowse enabled="true" />
   </system.webServer>

</location>
</configuration>

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38727303
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 php.net web site to find the documentation on how to do all of this.  Start with the intro here: http://php.net/tut.php

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.
http://www.sitepoint.com/books/phpmysql5/

Best of luck with the project, ~Ray
0
 
LVL 9

Accepted Solution

by:
abolinhas earned 2000 total points
ID: 38727494
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:driven_13
ID: 38728660
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,

--d.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38728663
Is the question, "How can I write an HTML anchor tag?"
0
 

Author Comment

by:driven_13
ID: 38728666
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.

--d.
0
 

Author Comment

by:driven_13
ID: 38728685
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.

--d.
0
 

Author Comment

by:driven_13
ID: 38728712
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) { 
		openFile(file);
	});
});
</script>

Open in new window


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

Any ideas.....????

Thanx as always.

--d.
0
 

Author Comment

by:driven_13
ID: 38728742
Given enough time I can figure anything out.

Thanx.
0
 
LVL 9

Expert Comment

by:abolinhas
ID: 38729141
Play with php headers in your php script or with your htaccess file

http://stackoverflow.com/questions/1465573/forcing-to-download-a-file-using-php?answertab=active#tab-top
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question