PHP - 下载的次数 [英] PHP - Number of times downloaded

查看:103
本文介绍了PHP - 下载的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何显示一个计数器来计算文件下载次数?我以前见过。 下载450次。谢谢。

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.

推荐答案

不要让用户直接下载文件,而是通过如下所示的脚本...



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");
   }

?>

这篇关于PHP - 下载的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆