Forums | MacLife
You are not logged in.
#1 2005-06-23 7:39 pm
Recursive Directory Listings but....
I have been playing with the possibilities of creating a php based directory reader. So far so good I have successfully made one that reads directories. Here is the problem. When one clicks on a directory I want the directories inside this directory to be read. So for example to make it more clear.
Someone clicks on a directory called "Ruby".
Inside the ruby folder I want the directory "Apple Software Update" which may contain a ruby script for playing with the Apple Software Update UNIX command. The readme.txt file is read and the .php file which i will use a regex to detect these things will be outputed.
The real problem here is the recursive part. All help appreciated. Let me know if I have not explained this well.
The other thing is I may have some directories that go one level deeper than the majority of the directories. I hate Apaches directory reader because it doesn't validate. Drives me nuts. Furthermore I wanted to customize some cool features such as downloading files and so on with this.
So I am thinking I will start from scratch again but need a push in the right direction. I have been through the php documentation. I have been wondering though like on software sites a listing of the software with a description and a download link. Do any of you do something similar to help point me in the right direction. I want to make it as fast as possible and not some slow piece of junk.
What if I could output this in XML and use XSLT or something to display it the way I wanted? Not sure what the best way is for this?? This is probably not but its come to mind.
Last edited by Crontab (2005-06-23 7:42 pm)
Offline
#2 2005-06-23 7:53 pm
Re: Recursive Directory Listings but....
Had a Thought:
Could it be possible to just list all my software directories in one directory. No organization at all.
Then create a manual XML file with all the properties i need for organization and some how use it to dynamically create what i need?
Code:
<listing> <file> <directory>Apple Software Update</directory> <category>RUBY</category> <description>sldfjalsdjflasdjfl</description> </file> </listing>
Offline
#3 2005-06-23 11:52 pm
Re: Recursive Directory Listings but....
Here's a PHP-based directory browser I made to access my schoolwork from school a few years ago. (I had a nasty habit of leaving things on the printer. Unfortunately I also had a habit of saving things on the desktop instead of in the schoolwork directory where they belonged.) Here it is in all of its nasty glory: perhaps you can glean something from it.
Um... I'm not really sure how it works anymore. It passes pathinfo to the script.
Code:
<?
if (!isset($_SERVER['PHP_AUTH_USER']) || !($_SERVER['PHP_AUTH_USER'] == "miles" and $_SERVER['PHP_AUTH_PW'] == "password")) {
header('WWW-Authenticate: Basic realm="Schoolwork"');
header('HTTP/1.0 401 Unauthorized');
echo 'Authentication is required to access this schoolwork.';
exit;
}
header("Content-type: text/html; charset=utf-8");
echo '<'.'?xml version="1.0" encoding="utf-8"?'.'>';
$dir = 'schoolwork-dir/';
$url_dir = '/schoolwork/';
$array_num = 1;
$path_array = explode("/", $_SERVER['PATH_INFO']);
while(file_exists($dir . $path_array[$array_num]) and filetype($dir . $path_array[$array_num]) == "dir" and $path_array[$array_num] != "" and isset($path_array[$array_num])) {
$up_dir = str_replace("%2F","/",rawurlencode($url_dir));
$dir .= $path_array[$array_num] . "/";
$url_dir .= $path_array[$array_num] . "/";
$array_num++;
}
$dh = opendir($dir);
$i = 0;
while (($file = readdir($dh)) !== false) {
if (!(preg_match("/^\./", $file) || preg_match("/^Icon/", $file) || ($file == "icons") || ($file == "Binder") || ($file == "graphics"))) {
if (filetype($dir . $file) == "dir") {
$icon = "folder";
} else {
$icon = "cwk";
}
$name = str_replace(':', '/', $file);
$file_array[] = array($name, filemtime($dir . $file), ($icon == 'folder' ? '-' : filesize($dir . $file)), str_replace("%2F","/",rawurlencode($icon == 'folder' ? $url_dir . $file . '/' : '/' . $dir . $file)), $icon);
/*
if ($ext = strrpos($name, ".") and (strlen($name) - $ext) <= 8 and $ext > 32) {
$name = substr($name, 0, 32) . ".." . substr($name, $ext);
} elseif (strlen($file) > 32) {
$name = substr($name, 0, 32) . "...";
}
*/
$i++;
}
}
if ($array_num == 1) {$dir_name = "Schoolwork";}
else {$dir_name = str_replace(':', '/', $path_array[$array_num - 1]);}
if ($_GET['N']) {$sortby = 0; $ad = $_GET['N'];}
elseif ($_GET['D']) {$sortby = 1; $ad = $_GET['D'];}
elseif ($_GET['S']) {$sortby = 2; $ad = $_GET['S'];}
elseif ($dir == 'schoolwork-dir/') {$sortby = 0; $ad = 'A';}
else {$sortby = 1; $ad = 'D';}
$asc = $ad == 'A';
usort($file_array, "cmp");
function cmp($a, $b) {
global $sortby, $asc;
if ($a[$sortby] == $b[$sortby]) {
return 0;
}
if ($asc) {return ($a[$sortby] < $b[$sortby]) ? -1 : 1;}
else {return ($a[$sortby] > $b[$sortby]) ? -1 : 1;}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title><?= $dir_name ?></title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<style type="text/css">
body {
background: url(/schoolwork-dir/graphics/back.png) #356BAA;
}
div.window {
position: absolute;
height: 300px; width: 600px;
left: 2px; top: 10px;
}
div.titlebar, div.statusbar, div.contentstrip, div.content {
position: absolute;
padding: 0px; margin: 0px;
}
img { margin: 0px; }
div.titlebar {
text-align: center;
top: 0px; left: 0px;
width: 100%; height: 24px;
background-image: url(/schoolwork-dir/graphics/title-b.png);
}
span.title {
position: relative;
top: 4px;
font-family: "Lucida Grande", Geneva, Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
text-align: center;
vertical-align: middle;
}
div.statusbar {
text-align: center;
top: 0px; left: 0px;
left: 14px; right: 14px;
width: 572px; height: 20px;
background-image: url(/schoolwork-dir/graphics/stat-b.png);
}
span.status {
position: relative;
top: 3px;
font-family: "Lucida Grande", Geneva, Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
text-align: center;
}
div.contentstrip {
top: 24px; bottom: 23px;
width: 100%; height: 253px;
background: #ffffff
}
div.content {
top: 20px;
left: 14px; right: 14px;
width: 572px; height: 233px;
overflow: auto;
background: #ffffff
}
div.sideshadow {
position: absolute;
width: 14px; height: 100%;
}
div.bottomshadow {
position: absolute;
bottom: 0px; left: 0px;
width: 100%; height: 23px;
background-image: url(/schoolwork-dir/graphics/bot-b.png);
}
table.filetable {
width: 100%;
border: 0px;
}
table.filetable th img {
border: 0px;
}
table.filetable th {
position: relative;
height: 17px;
background-image: url(/schoolwork-dir/graphics/s-b-d.png);
}
table.filetable td {
padding: 2px 5px;
font-family: "Lucida Grande", Geneva, Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
}
table.filetable td a {
color: #000;
text-decoration: none;
}
table.filetable td a:hover {
background: #6a95ff;
}
table.filetable td img {
vertical-align: middle;
}
table.filetable td.name {
<? if ($sortby==0) print "background: #f5f9ff;\n" ?>
}
table.filetable td.date {
white-space: nowrap;
<? if ($sortby==1) print "background: #f5f9ff;\n" ?>
padding-left: 10px;
}
table.filetable td.size {
white-space: nowrap;
<? if ($sortby==2) print "background: #f5f9ff;\n" ?>
padding-right: 12px;
text-align: right;
}
.activesort, .inactivesort {
display: block;
position: relative;
width: 100%;
height: 17px;
}
.activesort {
background-image: url(/schoolwork-dir/graphics/s-b-a.png);
}
.inactivesort {
background-image: url(/schoolwork-dir/graphics/s-b-d.png);
}
</style>
</head>
<body>
<div class="window">
<div class="titlebar">
<img src="/schoolwork-dir/graphics/title-l.png" alt="" style="position: absolute; left: 0px; top: 0px;" />
<span class="title"><img src="/images/folder.png" alt="" style="vertical-align: middle;" /> <?= $dir_name ?></span>
<img src="/schoolwork-dir/graphics/title-r.png" alt="" style="position: absolute; right: 0px; top: 0px;" />
</div>
<div class="contentstrip">
<div class="sideshadow" style="left: 0px; background: url(/schoolwork-dir/graphics/s-l.png);"></div>
<div class="sideshadow" style="right: 0px; background: url(/schoolwork-dir/graphics/s-r.png);"></div>
<div class="statusbar">
<span class="status"><?= $i ?> items</span>
</div>
<div class="content">
<table class="filetable" cellspacing="0" cellpadding="0">
<tr class="sortbar">
<?
$f = ($sortby == 0);
$pathback = str_replace("%2F","/",rawurlencode($_SERVER['PHP_SELF']));
print '<th><a href="'.$pathback.'?N=' . (($f and !$asc) ? 'A' : 'D') . '" title="Sort by Name" class="'.($f?'':'in').'activesort">';
print '<img src="/schoolwork-dir/graphics/s-l-'.($f?'a':'d').'.png" alt="" style="position: absolute; left: 0px;" />';
print '<img src="/schoolwork-dir/graphics/s-n-'.($f?'a':'d').'.png" alt="[Name'.($f?($asc?' ▲':' ▼'):'').']" style="position: absolute; left: 25px;" />';
if ($f) print '<img src="/schoolwork-dir/graphics/s-r-' . ($asc?'a':'d') . '.png" alt="" style="position: absolute; right: 0px;" />';
print "</a></th>\n\n";
$f = ($sortby == 1);
print '<th><a href="'.$pathback.'?D=' . (($f and !$asc) ? 'A' : 'D') . '" title="Sort by Date" class="'.($f?'':'in').'activesort">';
if ($sortby != 0) print '<img src="/schoolwork-dir/graphics/s-l-'.($f?'a':'d').'.png" alt="" style="position: absolute; left: 0px;" />';
print '<img src="/schoolwork-dir/graphics/s-d-'.($f?'a':'d').'.png" alt="[Last Modified'.($f?($asc?' ▲':' ▼'):'').']" style="position: absolute; left: 11px;" />';
if ($f) print '<img src="/schoolwork-dir/graphics/s-r-' . ($asc?'a':'d') . '.png" alt="" style="position: absolute; right: 0px;" />';
print "</a></th>\n\n";
$f = ($sortby == 2);
print '<th><a href="'.$pathback.'?S=' . (($f and !$asc) ? 'A' : 'D') . '" title="Sort by Size" class="'.($f?'':'in').'activesort">';
if ($sortby != 1) print '<img src="/schoolwork-dir/graphics/s-l-'.($f?'a':'d').'.png" alt="" style="position: absolute; left: 0px;" />';
print '<img src="/schoolwork-dir/graphics/s-s-'.($f?'a':'d').'.png" alt="[Size'.($f?($asc?' ▲':' ▼'):'').']" style="position: absolute; right: 13px;" />';
print '<img src="/schoolwork-dir/graphics/s-'.($f?($asc?'r-a':'r-d'):'l-d').'.png" alt="" style="position: absolute; right: 0px;" />';
print "</a></th>\n\n";
?>
</tr>
<?
if ($url_dir != "/schoolwork/") print '<tr><td class="name"><a href="'.$up_dir.'" style="font-style: italic;"><img src="/images/folder.png" /> Parent Directory</a></td> <td></td><td> </td></tr>';
foreach($file_array as $file) {
print '<tr><td class="name"><a href="';
print $file[3];
print '"><img src="/images/';
print $file[4];
print '.png" alt="[';
print ($file[4] == 'folder' ? 'dir' : $file[4]);
print ']" /> ';
print $file[0];
print '</a></td><td class="date">';
print date("n/d/y", $file[1]);
print '</td><td class="size">';
if ($file[2] != "-") {
$suffix = array('bytes','KB','MB','GB','TB','PB','EB');
$index = floor(log($file[2]+1,1024));
print sprintf("%.0".($index<2 ? '0' : '2' )."f %s", $file[2]/pow(1024,$index), $suffix[$index]);
} else {
print "-";
}
print "</td></tr>\n\n";
}
?>
</table>
</div>
</div>
<div class="bottomshadow">
<img src="/schoolwork-dir/graphics/bot-l.png" alt="" style="position: absolute; left: 0px; top: 0px;" />
<img src="/schoolwork-dir/graphics/bot-r.png" alt="" style="position: absolute; right: 0px; top: 0px;" />
</div>
</div>
</body></html>At some point there was also a handler that would convert AppleWorks documents to plain text using a Perl script, but I forget how that comes in.
The reason the HTML is so intense is because I worked pretty hard to create a damn fine imitation of a Mac OS X Finder window (complete with drop shadow) that worked in IE5. 
Hope this is some help! 
Offline
#4 2005-06-24 6:32 am
Re: Recursive Directory Listings but....
A little javascript is all you need.
I did this a while back:
http://gipetto.dyndns.org/stuff/list_directory.phps
and with a little script pilfered from Scott, it works very well
Last edited by Gipetto (2005-06-24 10:38 am)
Offline
#5 2005-06-24 8:20 am
Re: Recursive Directory Listings but....
I'm not sure exactly what you're talking about, but I think you mean something where it outputs every file and folder in one directory. When you click on a file, it opens. When you click on a folder, it displays the contents of that folder. If so, just do it like this:
Print every file/folder as a link like the following: your_listing_script.php?file=the_file_name
When someone clicks on it, it looks at $_GET['file'] and checks whether it is a file or a folder. If it is the former, it redirects the browser to the file. If it is the latter, it does the same thing it did in the beginning, only with the GET variable you sent instead of /.
Sorry, I'm too lazy to write code for you. 
Offline
#6 2005-06-24 8:25 am
Re: Recursive Directory Listings but....
Miles wrote:
The reason the HTML is so intense is because I worked pretty hard to create a damn fine imitation of a Mac OS X Finder window (complete with drop shadow) that worked in IE5.
...and then forgot to provide the images...
Offline
#7 2005-06-24 11:14 am
Re: Recursive Directory Listings but....
Thank you, for all the posts. Let me try to explain it better this is hard to explain so I found some examples on some sites.
Actually first let me give you a short description about what my site is about. I am still in school but in two years I will be getting a job so this site is a central place for all my accomplishments in my chosen field. I am a network major but I want ot show employers I can do a lot more than that and am a very worthy programmer expecially when it comes to programming web sites. So the software directory represents a list of directories of all the different technologies and the things I have built for it. So php, mysql, perl, ruby, bash scripts, and more. Anyway this is that run down.
http://www.mjruschak.net/bin/
So first the user sees this right? Now here comes the hard part to explain.
I click on the php directory. However I want it read like this.
http://www.entropy.ch/software/welcome.html
I don't want it exactly like this though but the way he outputs his packages I want to be similar. The name and the description.
So inside the PHP directory you might have a calendar directory inside it contains calendar.php and readme.txt
readme.txt is read and then there are two links to download or view the calendar? I hope this makes sense. I did think about using a database but I don't think it would be ideal for this. I want to throw a piece of software in there and just have it update without fussing around with a database. I had a gallery database for awhile and its so much better now that I have my file based one now. I think there are some things databases just aren't good for this being one of them.
So the potential problem is it needs to read the contents of each directory inside php in order to output the information I need. With the code that has been provided I will fuss around with it tonight. I have a second job to go to now. Thanks for all your help so far. I hope I explained it more clearly this time.
Offline
#8 2005-06-24 11:50 am
- zakatak
- Member

- From: Kalamazoo
- Registered: 2004-08-05
- Posts: 565
Re: Recursive Directory Listings but....
Can the output of your webserver's directory listing be customized?
Offline
#9 2005-06-24 4:20 pm
Re: Recursive Directory Listings but....
Basseq is me, John Whittet.
(Finishing the remainder of the thought expressed in the post has been left as an exercise for the reader.)
Offline
#10 2005-06-24 4:27 pm
Re: Recursive Directory Listings but....
Yes, but you can't actually edit the output unless you own the server. You can change the header and footer though.
Offline
#11 2005-06-24 10:39 pm
- zakatak
- Member

- From: Kalamazoo
- Registered: 2004-08-05
- Posts: 565
Re: Recursive Directory Listings but....
what about an .htaccess file? Does your host allow you to use them?
Offline
#12 2005-06-24 10:53 pm
- zakatak
- Member

- From: Kalamazoo
- Registered: 2004-08-05
- Posts: 565
Re: Recursive Directory Listings but....
*idea*
1. Make a script that reads/displays the contents of its own directory.
2. Make index.php scripts in each 'sub-directory' that include the script from step 1. (Could do nothing else even, just include that common file)
3. {I'm a bit hazy on the specifics so...}
4. Profit!
Offline
