PHP File Tree Re-post

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
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]
Sponsor our Newsletter | Privacy Policy | Terms of Service