如何计数请求文件


How to count requests to file

例如,我在ftp上有文件http://site.com/download/file.zip

我直接从浏览器的地址选项卡请求下载。

如何统计该文件的请求数?

或者我如何删除这些请求的能力,使它们只能在php中工作?

如果任何人都可以通过输入URL来访问文件,那么除了阅读web服务器访问日志之外,您无法真正计算访问次数。

可以做的是:

  1. 使文件本身无法通过任何URL访问
  2. 编写一个PHP脚本,根据传递给它的查询参数"服务文件"
  3. 在脚本中保存计数
通常,步骤2中的脚本看起来像这样:
// Increase your "download count" by one
// $mimeType is the MIME type of the file you are serving
//     e.g. "application/octet-stream"
// $filename is the name that the browser will offer as a default
//     in the "save file" dialog
// $filepath is the real path of the file on your web server
header('Content-Type: '.$mimeType);
header('Content-Disposition: attachment; filename="'.$filename. '";' );
header('Content-Length: '.filesize($filepath));
readfile($filepath);
die;

您可以创建一个download.php文件来处理下载。我的意思是:

http://site.com/download.php?dl=file

在这样的文件中,你可以做任何你想做的事情(记录时间戳,增加下载次数…)。然后重定向下载文件

您可以删除直接访问.htaccess文件的能力:

<FilesMatch ~ "^file'.zip$">
    Deny from all
</FilesMatch>

如果你不是一个网络大师。您可以像@Ale所说的那样,将其放入一个文件中,然后创建该文件。在那个文件中,放入谷歌分析,然后从那里跟踪它。你将拥有一切,甚至包括他们在哪里,有多少不同的人……等等。