PHP - 下载次数

use*_*ser 1 php counter download

如何显示计算文件下载次数的计数器?我以前见过它."下载了450次".谢谢.

ty8*_*812 6

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

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

?>
Run Code Online (Sandbox Code Playgroud)

  • 另外,在创建自定义下载脚本时,请注意不要下载/ etc/passwd等文件. (4认同)