PDA

View Full Version : how to view directory tree of an open source directory in php



goldd1gger
12-19-2007, 09:17 PM
I have been ask to create a php page where it display a directory tree (file manager type) of a server, but I have no idea where to begin.

Let me see if I can explain this. First, I need the page to point to an url/webpage where it will display the directory/folder for the server. It is a directory tree where you can view folder, subfolders, etc and their contents. And you can move back and forth in the directory tree.

As you go from directory to subdirectory, I want to be to capture that information so that when I click on a file name, I get the whole path in a variable. How do I do that?

For example:

If the website on the directory tree is http://webfile

and the final file name I want is: http://webfile/folder/subfolder/directory/filename.txt


:confused:

sfowler
12-20-2007, 09:00 PM
This website can help you set up a colapsible set of lists:
http://http://developer.yahoo.com/yui/treeview/#style

From here (http://us3.php.net/manual/en/ref.dir.php) look at scandir() and readdir(). There is also a whole slew of functions here (http://us3.php.net/manual/en/ref.filesystem.php) that will help. Use these to populate the tree levels.

After that, you can either use the parent/child relationships in the HTML DOM with Javascript, or you can add up the structure as you navigate your way down with the php functions and apply the whole thing to the final name.

Twey
12-20-2007, 09:20 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<?php
function paintUndersideOfFox($c = '.', $wcwd = false) {
if($wcwd === false)
$wcwd = substr($wcwd = $_SERVER['REQUEST_URI'], 0, strrpos($wcwd, '/') + 1);

echo('<ul class="dirlist">');
$d = opendir($c);
while($f = readdir($d)) {
if(strpos($f, '.') === 0) continue;

$ff = $c . '/' . $f;
echo '<li><a href="' . $wcwd . $ff . '">' . $f . '</a>';
if(is_dir($ff)) paintUndersideOfFox($ff, $wcwd);
echo '</li>';
}
echo('</ul>');
}
?>
<title>Directory Listing</title>
<style type="text/css">
ul.dirlist, ul.dirlist li {
list-style-type: none;
padding-left: 1em;
}
</style>
</head>
<body>
<?php
paintUndersideOfFox();
?>
</body>
</html>

conceptsottawa
06-04-2010, 02:30 AM
Do I save this as a PHP file? I get a server 500 error.

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster@computerconcepts.ca and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

djr33
06-04-2010, 03:24 AM
Yes, that is a .php file.

It sounds like your server is configured incorrectly. Make sure you have the right location (same location you uploaded for the URL), but then look into .htaccess and other methods that might affect how the directory structure works. 404 means it doesn't exist, so that means the file is either not there, or .htaccess (mod_rewrite probably) is not allowing it.

Or maybe php files are not allowed on your server. Try another location (a different directory) and if you can't get it to work, ask your host.