[英]How to send a string as file to the browser using php?
我想在內存中創建一個文件,並在單擊按鈕時將其發送到瀏覽器。 我期待以下代碼來做到這一點:
<?php
$content = 'This is supposed to be working... dammit';
$length = strlen($content);
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename=testfile.txt');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . $length);
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
header('Pragma: public');
echo $content;
exit;
?>
相反,我得到了回聲的字符串。 什么是我的愚蠢新手錯誤?
您需要更改內容類型,這適用於我,在FF和Chrome上測試
<?php
$content = 'This is supposed to be working... dammit';
$length = strlen($content);
header('Content-Description: File Transfer');
header('Content-Type: text/plain');//<<<<
header('Content-Disposition: attachment; filename=testfile.txt');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . $length);
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
header('Pragma: public');
echo $content;
exit;
?>
“請記住,在發送任何實際輸出之前必須調用header()”( c )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.