PHP - 计数下载

Uli*_*Uli 4 php

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

怎么做?谢谢你

gen*_*sis 11

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

header("Location: file.zip");
Run Code Online (Sandbox Code Playgroud)


luv*_*ere 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>
Run Code Online (Sandbox Code Playgroud)

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

download.php?Down=download.zip

回答参考Dreamincode回答类似的问题