[英]How to Stop Header, so I can echo to screen from the same function (PHP)
我發現Headers是一個噩夢,無法解決我的問題,我有一個用戶下載的文件,效果很好。 但是,我然后想在屏幕上回顯xx文件已發送; 但是該回聲只是放在發送的文件中。
完整的下載代碼如下:
function download()
{
$orderNo = $_POST['orderNo'];
$lines = file('F:/xamptest/htdocs/UniProject/upload/Amazon output.txt');
$lineCount = count($lines);
if($orderNo>0&&$orderNo<=$lineCount)
{
$lineEx = explode("\t", $lines[($orderNo-1)]);
$file = fopen('Order.txt', 'w');
fwrite($file, $lineEx[8].PHP_EOL .$lineEx[17].PHP_EOL .$lineEx[18].PHP_EOL .$lineEx[19].PHP_EOL .$lineEx[20].PHP_EOL .$lineEx[21].PHP_EOL .$lineEx[22].PHP_EOL .$lineEx[23].PHP_EOL);
fclose($file);
$file = 'Order.txt';
ob_end_clean();
header("Cache-Control: private");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
readfile($file);
echo "Current order, number: ".$orderNo."<br> Has been downloaded";
}
else
{
echo "Please enter a valid Order Number between 1 and ".$lineCount;
}
}
我似乎找不到不使用exit()停止標題的方法。 這仍然意味着它不會顯示回顯,並且不再使用ob_end_clean();。 以任何其他方式導致發送的文件為空。 我能想到的只有別的東西,在它自己的函數中有echo,但是當它同時運行時,標頭仍然將其放置在文件中。
非常感謝您的幫助-湯姆。
您正在處理的是一個HTTP請求 。 瀏覽器使用HTTP請求來請求 URL,服務器發送響應 。 單擊鏈接→瀏覽器發送請求→服務器發送HTML頁面作為響應。 使用Chrome,Safari或Firefox + Firebug,並在其Web檢查器/開發工具中使用“ 網絡”選項卡,以了解發生的事情。
如果要下載文件,則其工作方式相同。 只有服務器通過HTTP標頭告知瀏覽器,此響應不是普通的HTML頁面,而應該是應保存在磁盤上的文件。
如您所見,無法在一個響應中同時響應文件下載和 HTML頁面。 根本沒有辦法做到。 您必須先輸出帶有消息的HTML頁面,然后鏈接到從那里下載的文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.