下载计数器的exe文件在PHP


Download counter of exe file in PHP

我有一个计算文件下载的问题。如果下载文件的链接像这样:http://your.site.domain/download.php?file=filename.exe&customer=ABC_423,那就没有问题了。然而,我的客户想从这样的链接统计数据:http://your.site.domain/downloadsfolder/ABC_423/filename.exe,他还需要在其他网站上热链接该链接。

我认为在。htaccess文件中会有解决方案,但我不知道如何进行适当的重定向。

我的PHP脚本是这样的:
 
session_start();
define('SERVER_NAME', 'www.siteaddress.domain');
define('DOWNLOAD_PATH', 'http:// siteaddress.domain/versions/download');
define('DOWNLOAD_FILE', 'Setup.exe');
$filename = DOWNLOAD_PATH . '/' . $_GET['customer'] . '/' . DOWNLOAD_FILE;
$headers = get_headers($filename);
if ($headers[0] == 'HTTP/1.1 200 OK'){
  // file exists, begin download procedure
  if(isset($_GET['customer'])){
    // begin update statistics
    // mysql query to update specified row
  }
  // headers for downloading file
  header("Content-Type: application/force-download");
  header('Content-Type: application/x-unknown');
  header("Content-Type: application/octet-stream");
  //header("Content-Type: application/download"); // not sure if needed
  header("Content-Disposition: attachment; filename=".basename($filename).";");
  header("Accept-Ranges: bytes");
  header("Content-Transfer-Encoding: binary");
  header("Content-Length: ".remote_filesize(SERVER_NAME, $filename));
  ob_clean();
  flush();
  readfile($filename);
  exit;
}else{
  echo 'File not found';
  exit;
}

这是.htaccess中的一个简单的RewriteRule。像这样:

RewriteEngine On
RewriteRule   ^/downloadsfolder/(.*)/(.*)     /download.php?file=$2&customer=$1    [L]

观察如何使用(.*)检测随机字符序列,然后将$1$2用于翻译URL中的每个检测项目。

希望有帮助!