【问题标题】:download file then redirect to another page下载文件然后重定向到另一个页面
【发布时间】:2016-03-01 11:14:57
【问题描述】:

我需要下载一个 apk 应用程序,但我需要将用户保留在我的页面中或将他重定向到另一个页面 带有标题我只能下载应用程序或将他重定向到另一个页面,但我不能同时做这两个 我的 php 代码:

    $b= "location"; 



    echo "<script>window.location.href = '$b';</script>" ; 

            $file = 'apk.apk';

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

}

【问题讨论】:

标签: php file redirect download header


【解决方案1】:

@aron9forever 感谢您的帮助,我找到了一个好方法:

$e= "url.com" ; 



echo "<script>setTimeout(function(){window.location.href = '$e';} ,2)</script>" ; 


echo "<iframe src='download.php' ></iframe>" ; 

还有download.php:

<?php
$file = 'apk1.apk';

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

}
?>

【讨论】:

    【解决方案2】:

    根据我 5 分钟的研究。你的要求是不可能的。

    以下载内容然后重定向到“谢谢下载”的页面为例

    他们实际这样做的方式是将您带到“谢谢”页面,然后他们开始下载。

    要在您的代码中实现此功能,您需要先打开$b 位置,然后在该脚本中通过重定向到包含下载内容的 PHP 脚本来开始下载。

    例子:

    下载.php

    $file = 'apk.apk';
    
    if (file_exists($file))
    {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.basename($file).'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        readfile($file);
    }
    

    你好.php

    $b= "download.php"; 
    echo "Hello! <script>window.location.href = '$b';</script>" ; 
    

    当您访问 hello.php 时,您将看到 Hello!下载将开始

    【讨论】:

    • 我尝试这样做,但它下载了应用程序然后退出页面而不调用 javascript 代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-08
    • 2011-06-02
    • 2014-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多