Php 用户下载文件后,如何显示感谢页面?

Php 用户下载文件后,如何显示感谢页面?,php,html,Php,Html,我使用这个小pusher-php文件下载一个“exe”文件,供用户填写表单时保存 header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Transfer-Encoding: binary')

我使用这个小pusher-php文件下载一个“exe”文件,供用户填写表单时保存

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
使用此方法时,没有指向联机$file位置的链接可供其他人查看,但由于某些原因,我无法在代码执行后甚至在代码执行前立即显示感谢页面

我试过标题(“位置:$thankyouurl”);在上面的代码之后,但是没有显示任何内容,并且在运行上面的代码之前,我尝试回显感谢页面的html源代码,但是这会导致exe文件下载到网页,实际上会使浏览器崩溃


有什么建议吗?

将用户直接指向显示感谢信息的静态页面。然后,使用JavaScript将
窗口.location
设置为下载脚本

如果下载页面的标题设置正确,浏览器将开始下载文件,同时用户正在阅读您的感谢信息

确保显示直接下载链接,以防用户禁用JavaScript

例子: index.php

<a href="thankyou.php">Click here to download.</a>
<html>
<head>
    <script type="text/javascript">
        function startDownload() {
            window.location = "/download.php";
        }
    </script>
</head>
<body onload="startDownload();">
    <h1>Thank you!</h1>
    <p>Your download will start in a moment. If it doesn't, use this <a href="download.php">direct link.</a></p>
</body>
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
<a href="thankyou.php?download=12">Click here to download.</a>
<html>
<head>
    <script type="text/javascript">
        function startDownload() {
            window.location = "/download.php?file=<?=$_GET['download']?>";
        }
    </script>
</head>
<body onload="startDownload();">
    <h1>Thank you!</h1>
    <p>Your download will start in a moment. If it doesn't, use this <a href="download.php?file=<?=$_GET['download']?>">direct link.</a></p>
</body>
更新 为了将数据从
index.php
传递到下载脚本,可以使用
GET
变量。指向
thankyou.php
页面的链接将变成
thankyou.php?download=12
,其中download是您获取正确文件的ID。然后,通过在标记中回显该变量,将其传递到下载脚本,如下所示:

index.php

<a href="thankyou.php">Click here to download.</a>
<html>
<head>
    <script type="text/javascript">
        function startDownload() {
            window.location = "/download.php";
        }
    </script>
</head>
<body onload="startDownload();">
    <h1>Thank you!</h1>
    <p>Your download will start in a moment. If it doesn't, use this <a href="download.php">direct link.</a></p>
</body>
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
<a href="thankyou.php?download=12">Click here to download.</a>
<html>
<head>
    <script type="text/javascript">
        function startDownload() {
            window.location = "/download.php?file=<?=$_GET['download']?>";
        }
    </script>
</head>
<body onload="startDownload();">
    <h1>Thank you!</h1>
    <p>Your download will start in a moment. If it doesn't, use this <a href="download.php?file=<?=$_GET['download']?>">direct link.</a></p>
</body>

谢谢。php

<a href="thankyou.php">Click here to download.</a>
<html>
<head>
    <script type="text/javascript">
        function startDownload() {
            window.location = "/download.php";
        }
    </script>
</head>
<body onload="startDownload();">
    <h1>Thank you!</h1>
    <p>Your download will start in a moment. If it doesn't, use this <a href="download.php">direct link.</a></p>
</body>
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
<a href="thankyou.php?download=12">Click here to download.</a>
<html>
<head>
    <script type="text/javascript">
        function startDownload() {
            window.location = "/download.php?file=<?=$_GET['download']?>";
        }
    </script>
</head>
<body onload="startDownload();">
    <h1>Thank you!</h1>
    <p>Your download will start in a moment. If it doesn't, use this <a href="download.php?file=<?=$_GET['download']?>">direct link.</a></p>
</body>

函数startDownload(){
window.location=“/download.php?file=“;
}
非常感谢。
你的下载马上就要开始了。如果没有,就用这个


然后,您可以在下载脚本中获取值并以任何方式处理它。

对此不太确定,是否

header('Location: thanks_page.php');
…不工作,。如果放在readfile()之后?真的不确定,只有我的想法

您可以使用“windows.open('url to download file')打开“下载页面”

例如:

<script type="text/javascript">
    function openDownload()
    {
        window.open( "http://domain.com/download.php?file=file.pdf" );
        window.location = "http://domain.com/thanks.php";
    }
</script>

函数openDownload()
{
窗口打开(“http://domain.com/download.php?file=file.pdf" );
window.location=”http://domain.com/thanks.php";
}

打开一个直接下载文件的窗口,当您选择“保存”或“取消”时,该窗口将自动关闭,然后,“父”窗口将重定向到感谢页面。

使用原始方法,添加
标题('location:thank you page.php')结束,并有一个出口,如下所示:

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);

header('location: thank-you-page.php');
exit;

Thankyu页面是一个html,在readfile之后似乎什么都没有发生。如果你想强制下载该文件,为什么要使用
readfile
?只是想知道,另一个选项可能是包含标题(“位置:…”);在readfile()之前,即使在标头加载了感谢页面之后,脚本也可以继续运行,这只是一个选项,不是100%确定,但值得一试吗?可能在ob_clean()之前;还有,我想就是这个了。但是现在我如何将$file的值传递给download.php。在我的原始版本中,我使用表单发布和多个值来创建$file的值,这些值是由“include”下载文件伪传递的。我将您的想法与我的想法结合在一起,通过表单ost调用初始化php文件,该文件响应thatnk you页面的新版本,并在页眉中使用StartedDownload java脚本。现在我可以了;我不知道如何将$file值从js传递到php文件。谢谢您的支持。我正要尝试一下。谢谢页面显示出来,硬链接可以工作,但是startdownload功能似乎什么都不做。这就是它的完美之处。因为我使用了一个echo函数来创建ThankYou页面,所以我不需要$\u Get我的脚本调用看起来像函数startDownload(){window.location=“dwnldtrial.php?file=”.$file.“;}