# PHP File Tree Re-post

**URL:** <https://www.phphelp.com/t/php-file-tree-re-post/18435>\
**Category:** General PHP Help\
**Created:** [September 25, 2012, 8:51pm UTC](https://www.phphelp.com/t/php-file-tree-re-post/18435 "2012-09-25T20:51:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![special\_egg](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/85e7bf/32.png) [@special\_egg](https://www.phphelp.com/u/special_egg)\
**Post date:** [September 25, 2012, 8:51pm UTC](https://www.phphelp.com/t/php-file-tree-re-post/18435/1 "2012-09-25T20:51:35Z")

</div>

I would fisrt like to appologize to the forum if i made it seem like i was trying to get people to do my work for me. That simply was not the case. Anyway, here is the code snippett that creates the file tree itself:  
[php]\<?php

function php\_file\_tree($directory, $return\_link, $extensions = array(“doc”, “docx”, “xls”)) {  
if( substr($directory, -1) == “/” ) $directory = substr($directory, 0, strlen($directory) - 1);  
$code .= php\_file\_tree\_dir($directory, $return\_link, $extensions);  
return $code;  
}

function php\_file\_tree\_dir($directory, $return\_link, $extensions = array(), $first\_call = true) {  
if( function\_exists(“scandir”) ) $file = scandir($directory); else $file = php4\_scandir($directory);  
natcasesort($file);  
$files = $dirs = array();  
foreach($file as $this\_file) {  
if( is\_dir("$directory/$DocumentLibrary" ) ) $dirs[] = $DocumentLibrary; else $files[] = $DocumentLibrary;  
}  
$file = array\_merge($dirs, $files);

```
	if( !empty($extensions) ) {
	foreach( array_keys($file) as $key ) {
		if( !is_dir("$directory/$file[$key]") ) {
			$ext = substr($file[$key], strrpos($file[$key], ".") + 1); 
			if( !in_array($ext, $extensions) ) unset($file[$key]);
		}
	}
}

if( count($file) > 2 ) { // Use 2 instead of 0 to account for . and .. "directories"
	$php_file_tree = "<ul";
	if( $first_call ) { $php_file_tree .= " class=\"php-file-tree\""; $first_call = false; }
	$php_file_tree .= ">";
	foreach( $file as $DocumentLibrary ) {
		if( $DocumentLibrary != "." && $DocumentLibrary != ".." ) {
			if( is_dir("$directory/$DocumentLibrary") ) {
				$php_file_tree .= "<li class=\"pft-directory\"><a href=\"#\">" . htmlspecialchars($DocumentLibrary) . "</a>";
				$php_file_tree .= php_file_tree_dir("$directory/$DocumentLibrary", $return_link ,$extensions, false);
				$php_file_tree .= "</li>";
			} else {
		
				
				$ext = "ext-" . substr($DocumentLibrary, strrpos($this_file, ".") + 1); 
				$link = str_replace("[link]", "$directory/" . urlencode($DocumentLibrary), $return_link);
				$php_file_tree .= "<li class=\"pft-file " . strtolower($ext) . "\"><a href=\"$link\">" . htmlspecialchars($DocumentLibrary) . "</a></li>";
			}
		}
	}
	$php_file_tree .= "</ul>";
}
return $php_file_tree;

```

}

// For PHP4 compatibility  
function php4\_scandir($dir) {  
$dh = opendir($dir);  
while( false !== ($filename = readdir($dh)) ) {  
$files[] = $filename;  
}  
sort($files);  
return($files);  
}  
?\>[/php]The file path for the tree that I trying to create is [http://localhost/test\_site/documentLibrary](http://localhost/test_site/documentLibrary)  
I think I’m not referencing the file path correctly.  
Here is the page that references the php\_file\_tree.php file:

[php]\<?php

// Main function file  
include(“php\_file\_tree.php”);

?\>

```
	<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
	<link href="styles/default/default.css" rel="stylesheet" type="text/css" media="screen" />
	
	<!-- Makes the file tree(s) expand/collapsae dynamically -->
	<script src="php_file_tree.js" type="text/javascript"></script>
</head>

<body>

			
	<h2>Browse The Document Library...</h2>
	
	<?php
	
	// This links the user to http://example.com/?file=filename.ext
	echo php_file_tree($_SERVER['DOCUMENT_ROOT'], "http://NMPD_Website_Test/DocumentLibrary/?file=[link]/");

	$allowed_extensions = array("doc", "docx", "xls", "gif", "jpg", "jpeg", "png");
	echo php_file_tree($_SERVER['DOCUMENT_ROOT'], "http://NMPD_Website_Test/DocumentLibrary/?file=[link]/", $allowed_extensions);
	

	echo php_file_tree("NMPD_Website_Test/DocumentLibrary/", "javascript:alert('You clicked on [link]');");
	
	?>
	
</body>

```

Any starightforward, uncomplicated feedback would be helpful.[/php]
