开发者_运维知识库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
$current_count = file_get_contents('count');
$f = fopen('count', 'w+');
fwrite($f, $current_count + 1);
fclose($f);
header("Location: file.zip");
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
精彩评论