PHP - Count downloads

后端 未结 2 1614
误落风尘
误落风尘 2020-12-17 04:33

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

相关标签:
2条回答
  • 2020-12-17 05:13

    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

    0 讨论(0)
  • 2020-12-17 05:25
    $current_count = file_get_contents('count');
    $f = fopen('count', 'w+');
    fwrite($f, $current_count + 1);
    fclose($f);
    
    header("Location: file.zip");
    
    0 讨论(0)
提交回复
热议问题