[pmwiki-users] Counting downloads

Robert Riebisch rr at bttr-software.de
Fri Sep 30 04:47:48 CDT 2005


Hello!

Why not change the 'attach' markup to provide a download counter upon
request? I have some simple PHP snippets from another guy
(http://www.mylittlehomepage.net), but I don't know how to integrate
these into PmWiki.

Prior to PmWiki I accessed these scripts with "<a
href="download.php?file=batts.zip">Source code</a> <?php echo
download_info("batts.zip"); ?>", which results in, e.g., "Source code
(5.1 Kbytes/142 downloads)".

This is my early Christmas wish! :-)

*** download.php ***
<?php

if (isset($_GET['file'])) {
  $data = file("downloads.dat");
  $row_count = count($data);
  $fp = fopen("downloads.dat", "w+");
  flock($fp, 2);
  for ($row = 0; $row < $row_count; $row++) {
    $parts = explode("|", $data[$row]);
    if ($parts[0] == $_GET['file']) {
      $downloads = (int)$parts[1] + 1;
      fwrite($fp, $parts[0]."|".$downloads."\n");
      $found = true;
    }
    else {
      fwrite($fp, trim($data[$row])."\n");
    }
  }
  if (empty($found))
    fwrite($fp, urlencode($_GET['file'])."|1\n");
  flock($fp, 3);
  fclose($fp);

  header("location: ".$_GET['file']);
}
else {
  header("location: .");
}

?>
***

*** download_info.php ***
<?php

function download_info($was)
{
  $data = file("downloads.dat");
  $entry_count = count($data);
  unset($downloads);
  for ($i = 0; $i < $entry_count; $i++) {
    $part = explode("|", $data[$i]);
    if ($part[0] == $was) {
      $downloads = $part[1];
      $found = true;
      break;
    }
  }
  if (isset($found))
    $downloads = trim($downloads);
  else
    $downloads = 0;
?>(<?php echo number_format(filesize($was) / 1024, 1, ", ", "."); ?>
Kbytes/<?php echo $downloads; ?> downloads)<?php
}
?>
***

Feel free to contact me off-list. :-)

Robert Riebisch
-- 
BTTR Software
http://www.bttr-software.de/




More information about the pmwiki-users mailing list