PHP - 计数下载

时间:2011-08-27 10:26:07

标签: php

我想用PHP计算文件下载量。下载编号应存储在.TXT文件中。

怎么做? 谢谢 乌利

2 个答案:

答案 0 :(得分:10)

$current_count = file_get_contents('count');
$f = fopen('count', 'w+');
fwrite($f, $current_count + 1);
fclose($f);

header("Location: file.zip");

答案 1 :(得分:8)

创建一个名为download.php的文件,其中包含以下内容:

<?php
 $Down=$_GET['Down'];
?>

<html>
 <head>
  <meta http-equiv="refresh" content="0;url=<?php echo $Down; ?>">
 </head>
 <body>

 <?php

  $filePath = $Down.".txt";

  // If file exists, read current count from it, otherwise, initialize it to 0
  $count = file_exists($filePath) ? file_get_contents($filePath) : 0;

  // Increment the count and overwrite the file, writing the new value
  file_put_contents($filePath, ++$count);

  // Display current download count
  echo "Downloads:" . $count;
 ?> 

 </body>
</html>

在另一页中放置一个链接,将该文件作为参数下载:

download.php?Down=download.zip

回答参考Dreamincode answer to a similar question