PHP File Tree
Written by Cory S.N. LaViska on June 16th, 2007
A PHP function that generates a valid, XHTML nested list of the specified directory. Includes a JavaScript extension that makes the entire list expand and collapse dynamically.
Overview
PHP File Tree is a PHP function that generates a valid, XHTML nested list of the specified directory. The script includes a JavaScript extension that makes the entire list expand and collapse dynamically.
Features
- Produces valid, semantic XHTML
- Degrades gracefully in browsers that don’t support JavaScript (entire list will still be displayed)
- Fully customizeable with CSS
- Ability to style file icons based on the extension
- Easy to implement...one PHP include and a function call produces the entire list
- JavaScript extension for dynamic effects is included with only one line of code
Compatibility
The PHP File Tree JavaScript extension is known to work in the following browsers, but expected to work in most others that support JavaScript, too:
- Internet Explorer 6 & 7
- Firefox 2
- Safari 3
- Opera 9
Demo
- PHP File Tree classic
- PHP File Tree with jQuery — much nicer :)
Download
Download the current version of PHP File Tree.
Usage
Generating a File Tree
A call to the php_file_tree() function will return a string containing the XHTML code of the specified directory. To output it directly to the page, precede the function call with an echo statement:
Filter by Extension
You can limit the types of files that will be displayed by specifying an array containing a list of file extensions to allow:
Passing $allowed as the $extensions parameter will return a list of directories and only show image files of the respective types.
Feedback
Use $return_link to specify the action you want done when the user clicks on a file name. Use [link] as the placeholder for the filename. For example,
would send the user to http://example.com/?url=filename.ext. This is highly extendable, considering you could even use javascript to handle the clicks:
Making the List Expand/Collapse Dynamically
Classic Method
Include php_file_tree.js into the head section of the appropriate page(s) as shown below:
All file trees generated by PHP File Tree will automatically collapse to the top level (as specified by $directory) and become dynamic. You can have more than one file tree functioning on one page — simply create another using php_file_tree().
jQuery Method
If you happen to be using the jQuery libary, you can add the same functionality as shown above with a lot less code. You’ll also get the famous jQuery animations for showing and hiding tree branches, which improves usability and looks a lot nicer.
To use jQuery with PHP File Tree, make sure you are already including the jQuery library (if you don’t have it, you can get it on the jQuery download page) and then include php_file_tree_jquery.js.
<script src="php_file_tree_jquery.js" type="text/javascript"></script>
Depending on the version of jQuery you download and whether or not you get the compresses/uncompressed version, the name of jquery.js will vary. For example, jQuery 1.2 compressed will be called jquery-1.2.pack.js while the uncompressed version will be called jquery-1.2.js. Also, if you are already including the library you do not need to include it again.
Customizing the Appearance of the File Tree
The list can be customized completely using CSS and includes a default theme to aid in customization. Specific file types can be styled based on extension, making the file tree appear more like a standard file browser on your operating system. See default.css to get started.


Comments
#1 Leo on Jun 27th, 2007
#2 John Greenbury on Jun 27th, 2007
#3 John Greenbury on Jun 27th, 2007
#4 John Greenbury on Jun 28th, 2007
#5 Cory S.N. LaViska on Jun 28th, 2007
#6 zeezan on Jul 10th, 2007
#7 Jorn on Jul 13th, 2007
#8 Jorn on Jul 13th, 2007
#9 ravish on Aug 14th, 2007
#10 bob on Aug 20th, 2007
#11 Adam Smasher on Sep 12th, 2007
#12 Tony on Sep 13th, 2007
#13 Dustin on Sep 15th, 2007
#14 Dustin on Sep 16th, 2007
#15 Cory S.N. LaViska on Sep 16th, 2007
#16 Dan on Sep 20th, 2007
#17 Cogmios on Sep 22nd, 2007
#18 Killer-D on Oct 2nd, 2007
#19 Steffi on Oct 4th, 2007
#20 Killer-D on Oct 4th, 2007
#21 summsel on Oct 8th, 2007
#22 puntopunto on Oct 9th, 2007
#23 Cory S.N. LaViska on Oct 9th, 2007
#24 puntopunto on Oct 10th, 2007
#25 nick on Nov 12th, 2007
#26 feha on Nov 22nd, 2007
#27 Keith on Nov 28th, 2007
#28 Keith on Dec 4th, 2007
#29 Keith on Dec 4th, 2007
#30 Viktor on Dec 16th, 2007
#31 Cory S.N. LaViska on Dec 16th, 2007
#32 Viktor on Dec 16th, 2007
#33 Keith on Dec 19th, 2007
#34 Dave on Jan 7th, 2008
#35 Tomek on Jan 8th, 2008
#36 Milos Sula on Jan 14th, 2008
#37 Milos Sula on Jan 14th, 2008
#38 Rem on Jan 24th, 2008
#39 Thomass on Feb 18th, 2008
#40 mike foskett on Feb 19th, 2008
#41 Alan on Feb 19th, 2008
#42 ladislav on Feb 27th, 2008
#43 Shelon Padmore on Mar 7th, 2008
#44 Erik on Mar 8th, 2008
#45 Cezar on Mar 8th, 2008
#46 Levi Lewis on Mar 14th, 2008
#47 Thomaas on Mar 19th, 2008
#48 Thomaas on Mar 19th, 2008
#49 Joe on Mar 19th, 2008
#50 Chris on Mar 21st, 2008
#51 Chris on Mar 21st, 2008
#52 Chris on Mar 21st, 2008
#53 CHRIS on Mar 22nd, 2008
#54 Chris on Mar 23rd, 2008
#55 Thomaas on Mar 26th, 2008
#56 Marco on Apr 6th, 2008
#57 Cory S.N. LaViska on Apr 6th, 2008
#58 Minh Tho on Apr 9th, 2008
#59 Jerry on Apr 12th, 2008
#60 Jerry on Apr 12th, 2008
#61 Jerry on Apr 12th, 2008
#62 Jerry on Apr 12th, 2008
#63 Himanshu Singh on May 1st, 2008
#64 arg on May 6th, 2008
#65 Cory S.N. LaViska on May 7th, 2008
#66 George on May 8th, 2008
#67 George on May 8th, 2008
#68 Louis on May 19th, 2008
#69 Tom Oldfield on May 30th, 2008
#70 JG on Jun 12th, 2008
#71 Shan on Jun 19th, 2008
#72 Thiyagarajan G on Jun 20th, 2008
#73 matt jennings on Jun 30th, 2008
#74 matt jennings on Jun 30th, 2008
#75 matt jennings on Jun 30th, 2008
#76 James on Jul 7th, 2008
#77 El Duce on Jul 9th, 2008
#78 Abishaik on Jul 10th, 2008
#79 Anilkumar on Jul 10th, 2008
#80 Cory S.N. LaViska on Jul 11th, 2008
#81 Saeed on Jul 12th, 2008
#82 Anil on Jul 13th, 2008
#83 Abishaik on Jul 13th, 2008
#84 Cory S.N. LaViska on Jul 14th, 2008
#85 Cherry on Jul 16th, 2008
#86 Cory S.N. LaViska on Jul 16th, 2008
#87 Matt on Jul 17th, 2008
#88 Natalie on Jul 17th, 2008
#89 zahid on Jul 18th, 2008
#90 dinonet on Jul 23rd, 2008
#91 Kustan on Jul 23rd, 2008
#92 Jeff Gates on Jul 24th, 2008
#93 Matthias on Jul 29th, 2008
#94 vinod on Jul 31st, 2008
#95 vinod on Aug 1st, 2008
#96 gimley on Aug 4th, 2008
#97 Thomass on Aug 14th, 2008
#98 sugata on Aug 20th, 2008