PHP - 计算下载量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7213940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - Count downloads
提问by Uli
I'ld like to count file downloads with PHP. The downloads number should be stored in a .TXT file.
我想用 PHP 计算文件下载量。下载编号应存储在 .TXT 文件中。
How that can be done? Thanks Uli
怎么做?谢谢乌利
回答by genesis
$current_count = file_get_contents('count');
$f = fopen('count', 'w+');
fwrite($f, $current_count + 1);
fclose($f);
header("Location: file.zip");
回答by luvieere
Create a file named, say, download.php
, with the following content:
创建一个名为 的文件,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>
Put a link to it in another page, with the file to be downloaded as a parameter:
在另一个页面中放置一个指向它的链接,并将要下载的文件作为参数:
download.php?Down=download.zip
download.php?Down=download.zip
Answer reference Dreamincode answer to a similar question