View Full Version : Beginner...display directory contents on web page
bflo_chick
08-26-2002, 10:52 AM
I'm very new at PHP and I need to know if there is a script, or a way to write a script, that will display the contents of a folder (directory) on my web, in a new page. In my site I have a folder and it is a virtual directory of our company forms. These are ever changing, and I need a way to provide access to these forms company wide, but I would like to avoid a hard coded page because these forms change frequently and it would be impossible to keep up. I also have directories within this directory that I would like them to have the ability to browse. I pretty much want this page to work as if the user opened this folder from, say, their desktop, with the ability to open these documents and browse the directories. It is a important that this is dynamic because, for instance, a bunch of forms were added to this folder today. Like I said before, I am very new to PHP, I've only coded one page, a "e-mail contents of the form" page, so I need all the help I can get. Thanks!
8)
Don't most web servers have this functionality in them, already? I know apache does, because I use it. What webserver are you using? W/ regards to being dynamic, the only issue that comes to mind is that the browser has to be sure to truly reload the page, not just read it from its cache.
bflo_chick
08-26-2002, 02:33 PM
Yes, I do know that the servers have this feature, and I am using Apache, but I do not like the look of it, I would like it to look more windows like and was wondering if that could be acheived. My target audience is not the most computer literate, so it would be easier to keep things the way they know already.
Grizzly
08-26-2002, 03:25 PM
This is a skeletal version of a directory-reader I wrote a long time ago. It should be fairly self explanatory to use. Give it a shot and let me know if you have any problems.
<?php
// Setup your environment - these variables should be fairly self-explanatory
$baseDir='/my/directory/on/the/server/';
$httpPath='http://myserver/';
$dateMask="n/d/y";
$timeMask="g:i a";
$sortBy="name"; //Possible values are 'name', 'date', or 'size'
?>
<html>
<head>
<title>Directory Read</title>
<style type="text/css">
.header{font-family: tahoma,arial,times;font-size: 14px;font-weight: bold;color: #ffffef;background-color: #003366;font-variant: small-caps;}
.number{font-family: tahoma,arial,times;font-size: 10px;font-weight: bold;color: #003366;}
.smallcontent{font-family: tahoma,arial,times;font-size: 9px;color: #333333;}
td,.content {font-family: tahoma,arial,times;font-size: 12px;color: #333333;}
h2{font-family: tahoma,arial,times;font-size: 18px;color: #003366;}
a:link{color:#003366;text-decoration:underline;}
a:active{color:#003366;text-decoration:underline;}
a:visited{color:#003366;text-decoration:underline;}
a:hover{color:#003366;text-decoration:none;}
</style>
<body topmargin="10" leftmargin="10" marginwidth="10" marginheight="10">
<table width="450" cellpadding="1" cellspacing="0" border="0" align="center">
<tr>
<td class="header" width="40" align="center">#</td>
<td class="header">name</td>
<td class="header" width="80" align="right">size</td>
<td class="header" width="150" align="right">date created</td>
</tr>
<?php // Load the directory
if(!isset($sortBy)) $sortBy='date';
if($sortBy == 'name'){ $sortByVal=0; } else if($sortBy == 'size'){ $sortByVal=1;} else{ $sortByVal=2; }
loadDir($baseDir);
quickSort($fileArray,0,sizeof($fileArray)-1);
?>
<?php for( $i=0; $i<sizeof($fileArray); $i++ ){ ?>
<tr>
<td width="40" align="center"><?php printf("%0".strlen(sizeof($fileArray))."d",$i+1); ?></td>
<td><?php echo $fileArray[$i][0];?> (<?php echo $httpPath.$fileArray[$i][0]; ?>)</td>
<td width="80" align="right"><?php echo printFileSize($fileArray[$i][1]);?></td>
<td width="150" align="right"><?php echo date($dateMask,$fileArray[$i][2])."<span class=\"smallcontent\">".date($timeMask,$fileArray[$i][2])."</span>";?></td>
</tr>
<tr><td colspan="4">spacer.gif</td></tr>
<?php } ?>
</table>
</body>
</html>
<?php
// Functions - no need to edit below this line
function loadDir($inDir){
global $fileArray,$dirArray;
if ($dir = @opendir($inDir)) {
$fileArray=array(array());
$dirArray=array();
$i=0;
$j=0;
while (($file = readdir($dir)) != false) {
if(ereg('[^\.]+',$file)){
if(ereg('\.+',$file)){
$fileArray[$i][0]=$file;
$fileArray[$i][1]=filesize($inDir.$file);
$fileArray[$i][2]=filectime($inDir.$file);
$i++;
}else{ $dirArray[$j]=$file; $j++; }
}
}
} closedir($dir);
}
function quickSort(&$twoDimArray, $left, $right) {
global $sortByVal;
if ($left >= $right) return;
Swap($twoDimArray, $left, intval(($left+$right)/2));
$last = $left;
for ($i = $left + 1; $i <= $right; $i++) if ($twoDimArray[$i][$sortByVal] < $twoDimArray[$left][$sortByVal]) Swap($twoDimArray, ++$last, $i);
Swap($twoDimArray, $left, $last);
quickSort($twoDimArray, $left, $last-1);
quickSort($twoDimArray, $last+1, $right);
}
function Swap(&$Array, $i, $ii) {
$temp = $Array[$i];
$Array[$i] = $Array[$ii];
$Array[$ii] = $temp;
}
function printFileSize($size){
if($size>1048575){
printf("%10.2f",($size/1048576)); echo '<span class="smallcontent">MB</span>';
}elseif($size>1023){
printf("%10.2f",($size/1024)); echo '<span class="smallcontent">KB</span>';
}else{
printf("%10.2f",$size); echo '<span class="smallcontent">B</span>';
}
}
?>
Good luck!
DeadlySin3
08-26-2002, 09:10 PM
Grizzly,
I know I wasn't the one who requested this script, but I think I could find some good use of it sometime :) I think thats some great work you did there!
Grizzly
08-26-2002, 09:29 PM
No problemo :p I have a much 'beefier' version which I never got around to finishing. If my girlfriend ever lets me have free time again I might actually finish it someday, lol.
bflo_chick
08-27-2002, 08:54 AM
Thanks Grizzly! That is a great script! :)
DeadlySin3
08-28-2002, 11:03 PM
Hey Grizzly, would you mind if I were to offer that script as a download on PHPKit.Net (http://phpkit.net)? Of course, you would get full credit as author :)
Could I maybe take a looksee @ your 'beefier' version thats not yet done? I'd like to see what all you've done to it. Call me nosy. heh
-----
Note: If you do happen to go to phpkit - dont mind the mess there.. heh. I just got the site up and running - and i'll be working on her alot in the near future.
Grizzly
08-29-2002, 10:48 AM
DeadlySin3 - you can offer the script if you'd like. I personally don't yet consider it a finished product, as in, I wouldn't even consider posting it on freshmeat yet, but if you'd like to post it be my guest :p I'm sure some folks could find use for it.
The 'beefier' version I was speaking of is REALLY bare bones, and is nothing more than a sloppy, insecure skelton of what I'd like to build. It's more of a proof-of-concept than anything, but if you'd like you can check it out here:
http://grizz.dhs.org:81/dir/
It's highly insecure, sloppy, and untested right now. My vision is to allow you to browse a file system on any given server, and allow you to upload/download/rename/move files all over the place. I already have some solid upload architecture built, but I haven't gotten around to implementing it yet.
DeadlySin3
08-29-2002, 10:35 PM
Thats pretty good as well :) Let us know if you get the chance to update it!
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.