我试图用 PHP 创建一个下载计数器.我创建的脚本可以工作,但是当我单击下载链接时,脚本将我发送到一个空白页面.是否可以在下载和计数的同时停留在页面上?
I tried to create a download counter with PHP. The script, I have created, works, but when I click the download link, the script sends me to a blank page. Is it possible to stay on the page while downloading and counting?
这是我的 download.php 文件的代码:
Here's my code of my download.php file:
<?php
$Down=$_GET['Down'];
?>
<html>
<head>
<meta http-equiv="refresh" content="0;url=<?php echo $Down; ?>">
</head>
<body>
<?php
$fp = fopen("counter.txt", "r");
$count = fread($fp, 1024);
fclose($fp);
$count = $count + 1;
$fp = fopen("counter.txt", "w");
fwrite($fp, $count);
fclose($fp);
?>
</body>
</html>
这就是我的 index.php 中的链接的样子:
That's how the link in my index.php looks like:
<a href="download.php?Down=download.zip">download</a>
非常感谢!
好的,因为你缺少大部分代码,这里是一个例子,基本上你需要调用 download.php
文件,并在做计数器代码和设置下载头后传递文件内容.还要小心或允许恶意人员通过将文件名传递给下载功能来从您的服务器下载任何文件.download.php?file=index.php
等
Ok as you have most of the code missing here is an example, basically your need to call the counter code inside the download.php
file, and pass through the file contents after doing the counter code and setting download headers. also be wary or allowing malicious people to download any file from your server by just passing the file name to the download function. download.php?file=index.php
ect
<?php
function get_download_count($file=null){
$counters = './counters/';
if($file == null) return 0;
$count = 0;
if(file_exists($counters.md5($file).'_counter.txt')){
$fp = fopen($counters.md5($file).'_counter.txt', "r");
$count = fread($fp, 1024);
fclose($fp);
}else{
$fp = fopen($counters.md5($file).'_counter.txt', "w+");
fwrite($fp, $count);
fclose($fp);
}
return $count;
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
</head>
<body>
<a href="./download.php?file=exampleA.zip">exampleA.zip</a> (Downloaded <?php echo get_download_count('exampleA.zip');?> times)<br>
<a href="./download.php?file=exampleB.zip">exampleB.zip</a> (Downloaded <?php echo get_download_count('exampleB.zip');?> times)<br>
</body>
</html>
download.php 如您所见,它不输出 HTML,因为这会损坏文件.
download.php as you can see it outputs no HTML as this would corrupt the file.
<?php
//where the files are
$downloads_folder = './files/';
$counters_folder = './counters/';
//has a file name been passed?
if(!empty($_GET['file'])){
//protect from people getting other files
$file = basename($_GET['file']);
//does the file exist?
if(file_exists($downloads_folder.$file)){
//update counter - add if dont exist
if(file_exists($counters_folder.md5($file).'_counter.txt')){
$fp = fopen($counters_folder.md5($file).'_counter.txt', "r");
$count = fread($fp, 1024);
fclose($fp);
$fp = fopen($counters_folder.md5($file).'_counter.txt', "w");
fwrite($fp, $count + 1);
fclose($fp);
}else{
$fp = fopen($counters_folder.md5($file).'_counter.txt', "w+");
fwrite($fp, 1);
fclose($fp);
}
//set force download headers
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$file.'"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . sprintf("%u", filesize($downloads_folder.$file)));
//open and output file contents
$fh = fopen($downloads_folder.$file, "rb");
while (!feof($fh)) {
echo fgets($fh);
flush();
}
fclose($fh);
exit;
}else{
header("HTTP/1.0 404 Not Found");
exit('File not found!');
}
}else{
exit(header("Location: ./index.php"));
}
?>
确保您检查 $downloads_folder
变量是否正确.希望有帮助.
Make sure you check the $downloads_folder
variable is correct. Hope it helps.
下载完整示例代码.强>
这篇关于PHP 在不离开当前页面的情况下制作下载计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!