gpt4 book ai didi

PHP在不离开当前页面的情况下制作下载计数器

转载 作者:行者123 更新时间:2023-12-05 02:23:18 26 4
gpt4 key购买 nike

我试图用 PHP 创建一个下载计数器。我创建的脚本可以运行,但是当我单击下载链接时,脚本会将我带到一个空白页面。是否可以在下载和计数的同时停留在页面上?

这是我的 download.php 文件的代码:

<?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 中的链接是这样的:

<a href="download.php?Down=download.zip">download</a>

非常感谢!

最佳答案

好的,因为这里缺少大部分代码是一个示例,基本上您需要调用 download.php 文件中的计数器代码,并在执行计数器代码后传递文件内容并设置下载 header 。还要小心或允许恶意人员通过将文件名传递给下载功能来从您的服务器下载任何文件。 download.php?file=index.php

<?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,因为这会损坏文件。

<?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 变量是否正确。希望对您有所帮助。

Download Full example code.

关于PHP在不离开当前页面的情况下制作下载计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23329531/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com