我想用PHP计算文件下载量。下载编号应存储在.TXT文件中。
怎么做? 谢谢 乌利
答案 0 :(得分:10)
$current_count = file_get_contents('count');
$f = fopen('count', 'w+');
fwrite($f, $current_count + 1);
fclose($f);
header("Location: file.zip");
答案 1 :(得分:8)
创建一个名为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>
在另一页中放置一个链接,将该文件作为参数下载:
download.php?Down=download.zip