如何显示计算文件下载次数的计数器?我以前见过它。 “下载450次”。感谢。

有帮助吗?

解决方案

不要让用户直接下载文件,而是通过以下脚本...

<?php

   $file = 

不要让用户直接下载文件,而是通过以下脚本...

<*>REQUEST['file']; $dldir = "downloads/"; if ( (file_exists($dldir.$file) && // file exists (strpos($file, "../") === false) && // prevent an attacker from switching to a parent directory ) { header('Content-type: '.mime_content_type($dldir.file)); header("Content-Transfer-Encoding: binary"); header("Content-Length: " . filesize($dldir.$file) ."; "); header('Content-Disposition: attachment; filename="'.$file.'"'); echo file_get_contents($dldir.$file); /** Update the counter here, e.g. by using mysql **/ } else { die("File not found"); } ?>

其他提示

如果您想使用PHP,则需要在PHP脚本中控制下载。基本上它归结为以下两行伪代码:

set_number_of_downloads(get_number_of_downloads() + 1);
readfile($file_being_downloaded);

你去了。 此外,如果您更喜欢使用MySQL进行持久化,那么这个解决方案。

在Apache上,你可以让mod_rewrite在请求文件时更新数据库。这具有发送速度快的优点(可以使用sendfile),而且您不必更改URL或目录结构。

#!/usr/bin/perl
$| = 1;
$dbh = DBI->connect("dbi:mysql:database=; host=localhost; user=; password=")
or die "Connecting from PHP to MySQL database failed: $DBI::errstr";
while (<STDIN>) {
    $dbh->query(... update database ...);
    print 

在Apache上,你可以让mod_rewrite在请求文件时更新数据库。这具有发送速度快的优点(可以使用sendfile),而且您不必更改URL或目录结构。

<*>

http://httpd.apache.org/docs/2.2 /mod/mod_rewrite.html#rewriteengine

; }

http://httpd.apache.org/docs/2.2 /mod/mod_rewrite.html#rewriteengine

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top