【问题标题】:how to redirect to linkpage after download下载后如何重定向到链接页面
【发布时间】:2015-05-09 12:10:08
【问题描述】:

我正在使用一个简单的下载计数器来计算 txt 文件中的下载量。 在文件夹 downloads 我有一个名为 download.php 的文件,其中包含以下代码:

<?php
$Down=$_GET['Down'];
?>

<html>
<head>
<meta http-equiv="refresh" content="0;url=<?php echo $Down; ?>"> 
</head>
<body>

<?php

$filePath = $Down.".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>

我使用的链接位于下载文件夹之外的文件中,如下所示:

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

到目前为止,这工作正常。 单击链接后,网址给了我:http://localhost/downloads/download.php?Down=download.zip,然后下载了 zip 文件。

我想要实现的目标:立即重定向回链接页面;我在 download.php

中的回声后添加了标头位置
// Display current download count
//echo "Downloads:" . $count;
header('Location: http://localhost/link.php');

现在他立即返回并在文本文件中增加 1,但不再下载 zip 文件。 为什么重定向后文件不再下载?

【问题讨论】:

  • header() 开始之前不要回显任何内容,您好像禁用了error_reporting?
  • 我已经禁用了回声,因为它不再有用了。但问题是重定向后文件不再下载...
  • 试试这个... header('Content-Disposition: attachment; filename="'.$Down.'"');
  • @RJ Anoop 这很好用!现在他下载了文件。Thnx

标签: php


【解决方案1】:

主要的方法是先进入“之后”页面,然后开始下载。在您的情况下,您想将用户重定向到link.php,因此您需要首先将页面重定向到link.php,其中应该有html刷新meta-equiv标签与您的真实下载网址,它将下载文件并赢得'不影响当前页面。

由于您的前后页面相同,您可以使用link.php?download=true 传递参数。如果下载为true,则打印meta-equiv 标签。

我希望你能得到基本的想法。

【讨论】:

    【解决方案2】:

    尝试以下方法下载文件。

    header('Content-Disposition: attachment; filename="'.$Down.'"');
    

    【讨论】:

    • filename 不应被引用。
    【解决方案3】:

    我使用了另一种方法,用户点击下载链接页面上的链接,并且在不离开当前页面的情况下,计数器会实时递增。所以不需要重定向到另一个或相同的页面,因为文件被透明地计算而不会离开当前页面。下载计数器,恕我直言,应该是透明的,所以它更安全,没有人应该直接访问 php 脚本。另请注意,我的首选方法是使用文件扩展名限制操作,以便用户只能下载某些文件,另请注意,更好的方法是使用 .htaccess 拦截匹配的文件并将它们传递给 php 脚本......最后我也集成到我的项目 jquery 所以计数器是通过 ajax 实时更新的......所以不需要重新加载页面来查看结果......

    .htaccess(位于根目录/文件/)

    RewriteEngine on
    RewriteRule ^(.*).(rar|zip)$ /php/doing_download.php?file=$1.$2 [R,L]
    

    doing_download.php(位于 root/php 中)

    <?php
      $downloads_folder = $_SERVER['DOCUMENT_ROOT'] . '/files/';
      $counters_folder = $_SERVER['DOCUMENT_ROOT'] . '/files/counter/';
      if (!empty($_GET['file'])) {
        $file = basename($_GET['file']);
        $type = array("zip", "rar");
        $exts = strtolower(substr(strrchr($file, "."), 1));
        if (!in_array($exts, $type)) {
          header("HTTP/1.0 403 Forbidden");
          exit('File not allowed!');
        } else {
          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-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)));
            $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!');
          }
        }
      }
    ?>
    

    count_download.php(位于root/php/)

    <?php
      function get_download_count($file = null) {
        $counters = $_SERVER['DOCUMENT_ROOT'] . '/files/counter/';
        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;
      }
    ?>
    

    call_download.php(位于根目录/php)

    <?php
      include($_SERVER["DOCUMENT_ROOT"] . "/php/count_download.php");
      $item['item1'] = get_download_count('exampleA.zip');
      $item['item2'] = get_download_count('exampleB.zip');
      echo json_encode($item);
    ?>
    

    static_download.php(位于根目录/php)

    <?php
      include($_SERVER["DOCUMENT_ROOT"] . "/php/count_download.php");
      $item['item1'] = get_download_count('exampleA.zip');
      $item['item2'] = get_download_count('exampleB.zip');
    ?>
    

    download.js(位于root/jsc/)

    $(document).ready(function() {
      $.ajaxSetup({cache: false});
      getStatus();
    });
    function getStatus() {
      $.getJSON('php/call_download.php', function(data) {
        $('#item1').html(data.item1);
        $('#item2').html(data.item2);
      });
      setTimeout("getStatus()",1000);
    }
    

    index.php(位于根目录/)

    <?php include($_SERVER["DOCUMENT_ROOT"] . "/php/static_download.php"); ?>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Download Page</title>
    <script type="text/javascript" src="jsc/jquery.min.js"></script>
    <script type="text/javascript" src="jsc/download.js"></script>
    </head>
    <body>
    File: <a href="files/exampleA.zip">exampleA.zip</a>&nbsp; - &nbsp;Downloaded <span id="item1"><?php echo $item['item1']; ?></span> Times<br />
    File: <a href="files/exampleB.zip">exampleB.zip</a>&nbsp; - &nbsp;Downloaded <span id="item2"><?php echo $item['item2']; ?></span> Times<br />
    File: <a href="test/test.zip">test.zip</a><!-- this file never will be counted since is located in other folder --><br />
    </body>
    </html>
    

    Here, you can download all files and test them!

    基于 Lawrence Cherone 和我的回答:https://stackoverflow.com/a/29105023/4432311

    PS:您可能需要更改 php 脚本中的文件夹...我的脚本假定您正在服务器的根目录中进行测试...

    【讨论】:

    • 我很高兴听到您发现我的回答很有用 :)
    猜你喜欢
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 2022-06-18
    • 2014-10-06
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多