<small id='ep4nG'></small><noframes id='ep4nG'>

<i id='ep4nG'><tr id='ep4nG'><dt id='ep4nG'><q id='ep4nG'><span id='ep4nG'><b id='ep4nG'><form id='ep4nG'><ins id='ep4nG'></ins><ul id='ep4nG'></ul><sub id='ep4nG'></sub></form><legend id='ep4nG'></legend><bdo id='ep4nG'><pre id='ep4nG'><center id='ep4nG'></center></pre></bdo></b><th id='ep4nG'></th></span></q></dt></tr></i><div id='ep4nG'><tfoot id='ep4nG'></tfoot><dl id='ep4nG'><fieldset id='ep4nG'></fieldset></dl></div>
    1. <legend id='ep4nG'><style id='ep4nG'><dir id='ep4nG'><q id='ep4nG'></q></dir></style></legend>

        • <bdo id='ep4nG'></bdo><ul id='ep4nG'></ul>
      1. <tfoot id='ep4nG'></tfoot>

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

        时间:2023-08-20
      2. <i id='3aUYl'><tr id='3aUYl'><dt id='3aUYl'><q id='3aUYl'><span id='3aUYl'><b id='3aUYl'><form id='3aUYl'><ins id='3aUYl'></ins><ul id='3aUYl'></ul><sub id='3aUYl'></sub></form><legend id='3aUYl'></legend><bdo id='3aUYl'><pre id='3aUYl'><center id='3aUYl'></center></pre></bdo></b><th id='3aUYl'></th></span></q></dt></tr></i><div id='3aUYl'><tfoot id='3aUYl'></tfoot><dl id='3aUYl'><fieldset id='3aUYl'></fieldset></dl></div>

            <tbody id='3aUYl'></tbody>
            • <small id='3aUYl'></small><noframes id='3aUYl'>

              <tfoot id='3aUYl'></tfoot>

                  <bdo id='3aUYl'></bdo><ul id='3aUYl'></ul>
                  <legend id='3aUYl'><style id='3aUYl'><dir id='3aUYl'><q id='3aUYl'></q></dir></style></legend>
                • 本文介绍了PHP 在不离开当前页面的情况下制作下载计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我试图用 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 在不离开当前页面的情况下制作下载计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                • <legend id='5FiXw'><style id='5FiXw'><dir id='5FiXw'><q id='5FiXw'></q></dir></style></legend>

                  <tfoot id='5FiXw'></tfoot>
                    <tbody id='5FiXw'></tbody>

                  <i id='5FiXw'><tr id='5FiXw'><dt id='5FiXw'><q id='5FiXw'><span id='5FiXw'><b id='5FiXw'><form id='5FiXw'><ins id='5FiXw'></ins><ul id='5FiXw'></ul><sub id='5FiXw'></sub></form><legend id='5FiXw'></legend><bdo id='5FiXw'><pre id='5FiXw'><center id='5FiXw'></center></pre></bdo></b><th id='5FiXw'></th></span></q></dt></tr></i><div id='5FiXw'><tfoot id='5FiXw'></tfoot><dl id='5FiXw'><fieldset id='5FiXw'></fieldset></dl></div>
                    • <bdo id='5FiXw'></bdo><ul id='5FiXw'></ul>

                        <small id='5FiXw'></small><noframes id='5FiXw'>