我需要检查用户每天使用下载流量写代码php,并通过函数readfile()或任何检查下载文件?
function getTraffic($curl, $data)
{
$length = mb_strlen($data, '8bit');
$traffic += $length;
$write = fopen('traffic.txt', 'ab');
fwrite($write, $traffic);
fclose($write);
}
$file = "D:\aa.rar";
$curl = curl_init($file);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_WRITEFUNCTION, 'getTraffic');
echo curl_exec($curl);
答案 0 :(得分:0)
示例#1: php-mysql-download-counter
示例#2:
一些非常简单的PHP将是最好的选择。
<?php
$Down=$_GET['Down'];
?>
<html>
<head>
<meta http-equiv="refresh" content="0;url=<?php echo $Down; ?>">
</head>
<body>
<?php
$fp = fopen("Count.txt", "r");
$count = fread($fp, 1024);
fclose($fp);
$count = $count + 1;
//Un-comment to display the downloads.
echo "Downloads:" . $count . "";
$fp = fopen("Count.txt", "w");
fwrite($fp, $count);
fclose($fp);
?>
</body>
</html>
或强>
<?php
$Down=$_GET['Down'];
?>
<html>
<head>
<meta http-equiv="refresh" content="0;url=<?php echo $Down; ?>">
</head>
<body>
<?php
$filePath = 'count.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,然后将链接发送到&#39; download.php?Down = download.zip&#39; 很简单,下载次数将存储在文本文件中。
这篇文章已被常青检查
答案 1 :(得分:0)
尝试使用New Relic等工具。检查网址https://docs.newrelic.com/docs/php/new-relic-for-php
答案 2 :(得分:0)
我在考虑curl如何实现save-to-filestream功能。我意识到它必须像特殊的CURLOPT_WRITEFUNCTION,因为在保存到文件流时停止脚本会在我的网站空间上留下一个包含已加载部分的文件。
因此我用CURLOPT_WRITEFUNCTION尝试了它,它看起来并不像我想象的那样资源密集。
现在我使用register_shutdown_function调用一个保存已用流量的函数。我的CURLOPT_WRITEFUNCTION计算加载的数据=流量。
如果要将流量保存在文件中,将当前工作目录存储在变量中也很重要。因为在注册的关闭函数中调用的每个相对路径都与您的根目录无关,所以它相对于服务器根目录!您也可以使用绝对路径而不是cwd。
function readResponse($ch, $data) {
global $traffic;
$length = mb_strlen($data, '8bit');
//count traffic
$traffic += $length;
//output loaded data
echo $data;
return $length;
}
function saveTraffic($username, $cwd) {
global $traffic;
$h = fopen($cwd.'/relative-path/traffic_'.$username.'.txt', 'ab');
fwrite($h, $traffic);
fclose($h);
}
//...
$cwd = getcwd();
register_shutdown_function('saveTraffic', $username, $cwd);
$traffic = 0;
//...
//Output download header information to client
//...
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'readResponse');
//...
curl_exec($ch);