PHP在不离开当前页面的情况下制作下载计数器 [英] PHP making a download counter without leaving the current page

查看:128
本文介绍了PHP在不离开当前页面的情况下制作下载计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用PHP创建一个下载计数器。
脚本,我创建,工作,但是当我点击下载链接,脚本发送到一个空白页面。下载和计数是否可以留在页面上?



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

 <?php 
$ Down = $ _ GET ['Down'];
?>

< html>
< head>

< meta http-equiv =refreshcontent =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 文件中调用计数器代码,并在执行计数器代码并设置下载标题后通过文件内容。也要警惕或允许恶意人员通过将文件名传递到下载功能从服务器下载任何文件。 download.php?file = index.php ect

  php 
函数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-Typecontent =text / html; charset = utf-8>
< title>< / title>
< / head>

< body>

< a href =./ download.php?file = exampleA.zip> exampleA.zip< / a> (已下载<?php echo get_download_count('exampleA.zip');?> times)< br>
< a href =./ download.php?file = exampleB.zip> exampleB.zip< / a> (已下载<?php echo get_download_count('exampleB.zip');?> times)< br>

< / body>
< / html>

download.php ,您可以看到它不输出HTML,损坏文件。

 <?php 
//文件是
$ downloads_folder =' ./files/';
$ counters_folder ='./counters/';

//有一个文件名被传递了?
if(!empty($ _ GET ['file'])){
//保护人们获取其他文件
$ file = basename($ _ GET ['file']);

//文件是否存在?
if(file_exists($ downloads_folder。$ file)){

//更新计数器 - 添加如果不存在
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);
}

//设置强制下载头
标题('内容描述:文件传输');
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)));

//打开和输出文件内容
$ fh = fopen($ downloads_folder。$ file,rb);
while(!feof($ fh)){
echo fgets($ fh);
flush();
}
fclose($ fh);
退出;
} else {
header(HTTP / 1.0 404 Not Found);
exit('File not found!');
}
} else {
exit(header(Location:./index.php));
}
?>

确保检查 $ downloads_folder 变量是正确的。希望它有帮助。



下载完整示例代码。


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?

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>

That's how the link in my index.php looks like:

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

Many thanks in advance!

解决方案

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 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"));
}
?>

Make sure you check the $downloads_folder variable is correct. Hope it helps.

Download Full example code.

这篇关于PHP在不离开当前页面的情况下制作下载计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆