by Klaus Graefensteiner
1. April 2010 12:10
Einblick
My first real PHP application consists in just one page that looks for files in a specific folder on a web server and presents them as download links in a nicely styled table. For this quick solution I used a FTP connection to upload and manage the the content of the download folder.
The looks
I owe credits to Gunta Klavina, who designed the table CSS. I downloaded it from the CSS Table Gallery.
Figure 1: File download mini app in action
The script
The script is very simple. It goes to a specified directory and lists all files that are in there. Then it gets file size and last modified time from each of the files and adds them to an html table.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel=StyleSheet href="winterblues.css" type="text/css">
<title>Package File Download List</title>
</head>
<body>
<?php
$dir = "package/";
$files = scandir($dir);
$packagecount = count($files);
$odd =0;
?>
<table summary="Submitted table designs">
<caption>Available Packages</caption>
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Size</th>
<th scope="col">Version</th>
<th scope="col">Description</th>
<th scope="col">Download</th>
</tr>
</thead>
<tfoot>
<tr>
<th scope="row">Total</th>
<td colspan="4"><?php echo ($packagecount - 2) ?> Packages</td>
</tr>
</tfoot>
<tbody>
<?php
foreach($files as $file)
{
if($file != "." && $file != "..")
{
$name = basename($file);
$file = $dir.$file;
$date = date('j F Y H:i', filemtime($file));
$size = filesize($file).' bytes';
echo '<tr '; echo ($odd % 2)? 'class="odd">' : '>';
echo '<th scope="row">'.$name.'</th>';
echo '<td>'.$size.'</td>';
echo '<td>'.$date.'</td>';
echo '<td>Here is room for a detailed content description.</td>';
echo '<td><a href="'.$file.'" title="Download the '.$name.'">Download</a></td>';
echo '</tr>';'
$odd++;
}
}
?>
</table>
</body>
</html>
Download
The complete application including styles and test files can be downloaded here: FileDownloader.zip
Ausblick
That’s it.