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

Comments

4/3/2010 4:12:45 AM #

pingback

Pingback from topsy.com

Twitter Trackbacks for
        
        Tellingmachine | PHP mini application to present downloadable files
        [tellingmachine.com]
        on Topsy.com

topsy.com |

Comments are closed

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 and is founder of the PowerShell Unit Testing Framework PSUnit. More...

Open Source Projects

PSUnit is a Unit Testing framwork for PowerShell. It is designed for simplicity and hosted by Codeplex.
BlogShell is The tool for lazy developers who like to automate the composition of blog content during the writing of a blog post. It is hosted by CodePlex.

Administration

About

Powered by:
BlogEngine.Net
Version: 1.6.1.0

License:
Creative Commons License

Copyright:
© Copyright 2012, 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 2012 by Klaus Graefensteiner

Rendertime:
Page rendered at 2/7/2012 4:41:28 PM (PST Pacific Standard Time UTC DST -7)