PHP - 计数下载

问题描述:

我想用PHP计算文件下载量。下载号码应存储在.TXT文件中。PHP - 计数下载

这怎么办? 感谢 乌利

+2

你只需要一个文件或更多? –

+0

是的,只适用于一个文件。 (这是一个.zip文件) – Uli

+0

相关:http://*.com/questions/146/how-do-i-track-file-downloads-with-apache-php - 我特别喜欢grep'ing access.log – DanMan

创建一个名为,比如说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

回答参考Dreamincode answer to a similar question

+0

这看起来不错,但柜台不工作。我错过了什么? .txt文件以“0”计数和CHMOD 777开头。 – Uli

+0

它正在工作。谢谢! – Uli

+0

@Uli创建download.php文件后应该做什么? – aliboy38

$current_count = file_get_contents('count'); 
$f = fopen('count', 'w+'); 
fwrite($f, $current_count + 1); 
fclose($f); 

header("Location: file.zip");