PHP - counting downloads

I wanted to count file uploads using PHP. The download number must be stored in a .TXT file.

How can I do that? thanks Uli

+5
source share
2 answers

Create a file with the name, say, of the download.phpfollowing contents:

<?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>

Put a link to it on another page, the file will be downloaded as a parameter:

download.php?Down=download.zip

Answer the link Dreamincode answer to a similar question

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

header("Location: file.zip");
+10
source

All Articles