【问题标题】:Download counter of exe file in PHPPHP中exe文件的下载计数器
【发布时间】:2013-03-01 14:42:13
【问题描述】:

我在计算文件下载次数时遇到问题。如果下载文件的链接如下所示,则没有问题: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;
}

【问题讨论】:

    标签: php download counter


    【解决方案1】:

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

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

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

    希望有所帮助!

    【讨论】:

    • 我已经尝试过了,但我得到了循环,因为 download.php 中的标头函数再次重定向到该文件。所以 .htaccess 将无限循环重定向...
    • 请将DOWNLOAD_PATH 更改为本地路径,而不是 URL。这就是循环的原因。
    • 感谢您的宝贵时间和帮助。它对我帮助很大,我会试试你上面提到的这个方法。
    猜你喜欢
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 2010-09-19
    • 1970-01-01
    • 2017-11-05
    相关资源
    最近更新 更多