PHP - 计数下载
答
创建一个名为,比如说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
答
$current_count = file_get_contents('count');
$f = fopen('count', 'w+');
fwrite($f, $current_count + 1);
fclose($f);
header("Location: file.zip");
你只需要一个文件或更多? –
是的,只适用于一个文件。 (这是一个.zip文件) – Uli
相关:http://*.com/questions/146/how-do-i-track-file-downloads-with-apache-php - 我特别喜欢grep'ing access.log – DanMan