用PHP强制下载然后重定向 [英] Force download with PHP then redirect

查看:169
本文介绍了用PHP强制下载然后重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问过很多次,但是我找不到满足我需要的答案。

I know this question has been asked many times before but I can't find an answer to suit my needs.

我需要找到一种方法来强制下载一个文件,然后在下载开始之后,重定向到感谢下载页面。

I need to find a way to force the download of a file and then, after the download has started, redirect to a "thanks for downloading" page.

到目前为止,我有:

<?php
ob_start();

$token = $_POST['validationCode'];

if(isset($token)){

    $connect = mysql_connect('localhost', 'root', 'root');
    $db = mysql_select_db('mydb');

    if (!$connect || !$db){
        die('Connect Error (' . mysql_connect_errno() . ') '
                . mysql_connect_error());
    }

    $sql = mysql_query("SELECT * FROM emailaddresses WHERE token='$token'");
    $result = mysql_fetch_array($sql);
    if($result){
        header('Location: complete.php');
        header('Content-type: application/mp3');
        header('Content-Disposition: attachment; filename=track.mp3');
        $f = file_get_contents('downloads/track.mp3');
        print $f;
        $sql = "UPDATE emailaddresses SET download=1 WHERE token='$token'";
        $result = mysql_query($sql);
    }
    else{
        echo "There was a problem downloading the file" . mysql_error();
    }
}

ob_end_flush();

?>

重要的是隐藏下载文件的位置,否则我将刚刚创建了一个到该文件的HTML链接。

It's important to hide the download file's location otherwise I would have just created an HTML link to the file.

我显然无法将重定向头放在其他标题下方,因为它不会工作。除了在弹出窗口中打开这个窗口,并将主窗口指向谢谢页面,我看不出去哪里,但这是一个最后的度假村。

I obviously can't put a redirect header below the other headers as it just won't work. I can't really see where to go from here apart from opening this in a pop-up and directing the main window to the "thank you" page - but that is a LAST resort.

任何人都可以提供任何建议?

Can anyone give any suggestions?

干杯,

Rich

推荐答案


  1. 您不能隐藏文件位置。只要浏览器需要知道要下载文件的URL,那么任何人都可以确定,找到它是很明显的。

  2. 你不能用两个标题重定向,如你所说。您只能在使用Javascript进行某些超时后重定向到其他页面。

  1. You can't hide a file location. It'll be plainly visible to anybody determined enough to find it, by the very necessity that the browser needs to know the URL to download the file.
  2. You can't do it with two header redirects in succession, as you said. You can only redirect to a different page after some timeout using Javascript.

真的没有太多的选择。如果您的主要目标是隐藏网址,那么这是一个迷失的原因。为了良好的可用性,您通常还会在页面上包含纯粹的链接(下载不开始,点击这里 ...),因为用户可能会意外取消在错误的时间重定向不可撤销地杀死下载。

There really isn't much choice. If your primary goal is to hide the URL, that's a lost cause anyway. For good usability, you usually include the plain link on the page anyway ("Download doesn't start? Click here..."), since the user may accidentally cancel the redirect at just the wrong time to irrevocably kill the download.

您不能在文件本身输出任何东西相同的请求/响应。您可以尝试由@netcoder建议的多部分HTTP响应,但我并不确定支持的程度如何。只是假设有一个浪费的请求/响应,其中只有文件被下载,没有其他的事情发生。通常使用这种限制的方式是这样的:

You cannot output anything other than the file itself in the same request/response. You could try multi-part HTTP responses as suggested by @netcoder, but I'm not really sure how well that's supported. Just start with the assumption that there's one "wasted" request/response in which only the file is downloaded and nothing else happens. The way things usually work with this restriction is like this:


  • 用户点击下载链接或提交表单与他的电子邮件地址或任何需要启动下载过程。

  • 服务器返回感谢您从我们下载!您的下载将很快开始...页面。 >
  • 此页面包含Javascript或< meta> 刷新或HTTP 刷新标题导致延迟重定向到该文件的URL。

  • 谢谢页面将重定向到文件位置,但是由于这会导致文件下载,页面将不会显着更改,只有下载将被启动。

  • User clicks "download" link or submits form with his email address or whatever is required to initiate the download process.
  • Server returns the "Thank you for downloading from us! Your download will start shortly..." page.
  • This page contains Javascript or a <meta> refresh or HTTP Refresh header that causes a delayed redirect to the URL of the file.
  • The "Thank you" page will "redirect" to the file location, but since this causes the file to download, the page will not visibly change, only the download will be initiated.

查看< a href =http://download.com =nofollow> http://download.com ,以作为这个行动的一个例子。

Look at http://download.com for an example of this in action.

您可以使该文件的下载位置是只允许用户返回该文件的脚本下载文件您可以在谢谢页面和文件下载页面之间传递一些临时令牌,以验证是否允许下载。

You can make the download location for the file be a script that only returns the file if the user is allowed to download the file. You can pass some temporary token between the "Thank you" page and the file download page to verify that the download is allowed.

这篇关于用PHP强制下载然后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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