如何显示一个计算文件下载次数的计数器?我以前见过。“下载了 450 次”。谢谢。
问问题
2049 次
4 回答
7
不要让用户直接下载文件,而是通过如下脚本...
$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");
}
于 2009-10-27T11:11:07.077 回答
2
如果你想用 PHP 来做,你需要在 PHP 脚本中控制下载。基本上它归结为以下两行伪代码:
set_number_of_downloads(get_number_of_downloads() + 1);
readfile($file_being_downloaded);
于 2009-10-27T11:08:30.163 回答
0
在 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 $_;
}
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriteengine
于 2009-10-27T12:15:35.090 回答