簡體   English   中英

使用腳本寫入文件時,標題重定向advert die()或exit()

[英]Header redirect advert die() OR exit() when using script to write to file

我制作了一個小腳本,該腳本可以動態創建一個文本文件,然后在按下下載按鈕時將其下載到用戶計算機上...下面的整個腳本是用戶按下“下載”時運行的腳本

<?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"));

但是,使用此文件不會下載文件,但是會將用戶重定向

感謝您的幫助

使用Javascript打開執行下載的彈出窗口,然后重定向原始窗口。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM