Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Count downloads

Tags:

php

I'ld like to count file downloads with PHP. The downloads number should be stored in a .TXT file.

How that can be done? Thanks Uli

like image 307
Uli Avatar asked Aug 27 '11 10:08

Uli


2 Answers

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

header("Location: file.zip");
like image 165
genesis Avatar answered Sep 30 '22 06:09

genesis


Create a file named, say, download.php, with the following content:

<?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 in another page, with the file to be downloaded as a parameter:

download.php?Down=download.zip

Answer reference Dreamincode answer to a similar question

like image 38
luvieere Avatar answered Sep 30 '22 06:09

luvieere