当使用脚本写入文件时,标题重定向广告die()或exit()

时间:2017-10-09 21:34:38

标签: php

我创建了一个小脚本,动态创建了一个文本文件,然后按下下载按钮将其下载到用户计算机...下面的整个脚本是当用户按下'#39'时运行的脚本;

<?php 

    if (isset($_GET['download'])) {
        if ($_GET['download'] == "1") {


            header('Content-disposition: attachment; filename=TEST - Backup.txt');
            header('Content-type: text/plain');



            $projects = "SELECT * FROM projects WHERE user_id = '$user_id' ORDER BY id ASC"; 
            if ($result=mysqli_query($conn,$projects)) {
                while ($projects_array=mysqli_fetch_array($result)) { 


                    $sql = $conn->query("SELECT COUNT(*) FROM refs WHERE project_id = " . $projects_array['id']);
                    $row = $sql->fetch_row();
                    $count = $row[0];

                    echo "\n\n" . "=== " . $projects_array['project_name'] . " ===" . "\n\n";


                    if ($count > 0) {
                        $all = "SELECT * FROM refs WHERE user_id = '$user_id' AND project_id = " . $projects_array['id'] . " ORDER BY IF(a_s_name = '', company_a, a_s_name) ASC"; 
                        if ($result_a=mysqli_query($conn,$all)) {
                            while ($ref=mysqli_fetch_array($result_a)) {    
                                include 'sections/get.php';
                                if ($ref['type'] == 1){
                                    include 'refs/1.php'; 
                                } elseif ($ref['type'] == 2) {
                                    include 'refs/2.php'; 
                                } elseif ($ref['type'] == 3) {
                                    include 'refs/3.php'; 
                                }

                                echo "\t" . strip_tags($cit) . "\n";
                                echo "\t" . strip_tags($ref) . "\n\n";

                            }
                        }
                    } else {
                        echo "\t" . "You don't currently have any references for this project." . "\n\n";
                    }

                }
            }
            die ();


        }
    }

?>

用户按下下载按钮后,当前文件已下载,但我希望用户可以重定向到网站上的新位置。

我试图通过使用标题位置和die(); ...所以我用下面的替换die():

die (header ("Location: http://siteurl.co.uk/?suc=8"));

但是使用此文件时,不会下载文件,但会重定向用户

我感谢任何帮助

1 个答案:

答案 0 :(得分:1)

使用Javascript打开执行下载的弹出窗口,然后重定向原始窗口。

window.open("yourscript.php?download=1");
window.location.href = "http://siteurl.co.uk/?suc=8";