How can I display a counter that counts the number of times a file is downloaded? I've seen it before. "Downloaded 450 times". Thanks.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Don't let the user download a file directly, but trough a script like the following ...
<?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");
}
?>
回答2:
If you want to do it with PHP, you need to control the download in a PHP script. Basically it comes down to the following two lines of pseudo-code:
set_number_of_downloads(get_number_of_downloads() + 1);
readfile($file_being_downloaded);
回答3:
There you go. Also, if you prefer using MySQL for persistence, there's this solution.
回答4:
On Apache you could have mod_rewrite update a database when the file is requested. This has the advantage of speed in sending (sendfile can be used), and you don't have to change your URLs or directory structure.
#!/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