I am working for a client who wants me to set up a better way for him to distribute his press releases. Right now he has a FTP folder which just displays folders and files. He would like a HTML frontend that does the same thing but without having to change the code when he adds new items.
The way I am approaching this is putting an index.php file into each folder and then letting that read all the files and display them sortable by date, name, etc. using this script.
<?php
// Setup your environment - these variables should be fairly self-explanatory
$baseDir='/mnt/rw/var/www/vhosts/path/to/folder/';
$httpPath='/path/to/folder/';
$dateMask="n/d/y";
$timeMask="g:i a";
$sortBy="date"; //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"><img src="folder.gif" alt="" height="17" width="17" border="0"></td>
<td><a href="../../<?php echo $httpPath.$fileArray[$i][0]; ?>"><?php echo $fileArray[$i][0];?></a></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"><img src="../../spacer.gif" width="1" height="1" border="0"></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> ';
}
}
?>
The only problem is that it requires me to set the variables for basedir and httppath which won't work because I can't expect my client to do that every time he wants to upload a new folder.
I've seen scripts that can read out the directory content and linking to it without having to set any variables - I just can't figure out how to make it work.
Also if there are more effective ways of doing this without actually having to create my own content management system that would be great.
Andy