PHP mini application to present downloadable files

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.

Package File Download List - Mozilla Firefox

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.

Tags: , , ,

Php

About Klaus Graefensteiner

I like the programming of machines.

Add to Google Reader or Homepage

LinkedIn FacebookTwitter View Klaus Graefensteiner's profile on Technorati
Klaus Graefensteiner

Klaus Graefensteiner
works as developer in Test at Rockwell Automation and is founder of the PowerShell Unit Testing Framework PSUnit. More...

Administration

About

Powered by:
BlogEngine.Net
Version: 1.5.0.7

License:
Creative Commons License

Copyright:
© Copyright 2009, Klaus Graefensteiner.

Disclaimer:
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Theme design:
This blog theme was designed and is copyrighted 2009 by Klaus Graefensteiner

Rendertime:
Page rendered at 9/9/2010 7:28:17 PM (PST Pacific Standard Time UTC DST -7)