Log in using OpenIDCancel OpenID login

Downloads

Submitted by marble on Thu, 2006-05-18 11:07.
Version:0.4 Release Date:2008-01-11 Status:Beta (usable) Language:PHP

When I was setting up my new website, using Drupal, I wanted to have a page per project, like the one you're reading now. I wanted to attach the programs to the page, and have it show the MD5Sums and download count. The builtin upload.module didn't do that. Other modules did, but added more complexity, such as making each download its own node, so I wrote downloads.module.

On install, it creates a downloads_data table, and whenever a page is viewed, it checks to see if there are attachments listed which aren't in the table yet. If so, it calculates the MD5Sum and adds them. Either way, it presents the extra info in an array that the theme can use to add to the attachments table. For the counting, it hooks onto the file download hook, and updates the database.

Theming

Here's a snippet from my node-content-program.tpl.php file, showing how to use the extra variables:

<?php
$files 
$node->files;
$header = array(t('File'), t('Size'), t('MD5Sum'), t('Downloads'));
$rows = array();
foreach (
$files as $file) {
  if (
$file->list) {
    
$fid=$file->fid;
    
$dlinfo=$node->dls[$fid];
    
$href check_url(($file->fid file_create_url($file->filepath) : url(file_create_filename($file->filenamefile_create_path()))));
    
$text check_plain($file->description $file->description $file->filename);
    
$rows[] = array(l($text$href), format_size($file->filesize), $dlinfo->md5$dlinfo->count);
  }
}
if (
count($rows)) {
  echo 
theme('table'$header$rows, array('id' => 'attachments'));
}
?>

Most of that is just copied from the existing table creation code. The new bit is

<?php
$dlinfo
=$node->dls[$fid];
?>

and the use of that to add the extra two columns to

<?php
$rows
[]
?>

(My site adds an icon next to the filename, based on the filemime, but I cut that out from the above to keep it simple.)

Drupal versions

There's now a Drupal 5 version, with a bugfix and a .info file. Use downloads 0.3 for Drupal 4.7, or downloads-5.x-0.4 for Drupal 5.

FileSizeMD5SumDownloads
downloads-0.3.tar.bz21.27 KB0e847231c263dde9daff075010c2bfe385
downloads-5.x-0.4.tar.bz21.63 KB8f55b102d1d6728dc6a445a7462e3bc913
Posted in Submitted by marble on Thu, 2006-05-18 11:07.