<?php
/*
* Downloadcounter Kontrollscript
* Author Dirk Hoeschen (hoeschen at feenders.de) 
*/
$path="./downloads/";
// hole dateinamen aus variable f
$f = @strip_tags($_GET['f']);
if ($f!="") {
  // ganzes verzeichnis lesen
  $dir_handle = @opendir($path) or die("Unable to open ".$path);
  while ($file = readdir($dir_handle)) {
    if (!is_dir($file)) {
   if ($file==$f) { 
        rename($path.$file,$path."0:0:".$file);
        $file = "0:0:".$file; 
      }
       // datei in array splitten
      $fparts = preg_split('/:/',$file);
     if ((count($fparts)>1) && ($fparts[2]==$f)) {
      // counter+1, zufallswert ermitteln, datei umbenenenn
        $dlcount = intval($fparts[0])+1;
        $frandom = hash("md5",microtime());
        $newfilename=$dlcount.":".$frandom.":".$f;
        rename($path.$file,$path.$newfilename);
        break;
       }
    }     
  }
 
  // datei mit dem ursprünglichen namen ausgeben
  if (isset($newfilename) && ($newfilename!="")) {
    header('Content-type: application/zip');
    header('Content-Disposition: attachment; filename="'.$f.'"');
    header("Content-Transfer-Encoding: binary");
    readfile($path.$newfilename);
  }
 
}
echo $dlcount; 
?>
