1

我想在内存中创建一个文件,并在单击按钮时将其发送到浏览器。我期待以下代码可以解决问题:

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

?>

相反,我得到了字符串的回显。我愚蠢的新手错误是什么?

4

2 回答 2

4

你需要改变你的内容类型,这对我有用,在 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;
?>
于 2012-05-31T14:37:20.297 回答
0

“请记住,必须在发送任何实际输出之前调用 header()”(c

于 2016-02-23T10:15:32.027 回答