强制下载而不会丢失格式?

时间:2014-04-09 07:09:20

标签: php http-headers download force-download

我想从服务器下载文本文件到客户端,代码正常工作,但在我下载文本文件后,它会在顶部显示一个额外的空行。

如何删除该行或如何将文本文件的原始格式导入客户端计算机?

这是我的代码:

$code = $_POST["Code"];
$File = "my$code.txt"; 
if (file_exists($File))
{
    header("Content-Type:text/plain");
    header ("Content-Disposition: attachment; filename=$File");
    header("Content-Length:" . filesize("$File"));
    $fp = fopen("$File", "r");
    fpassthru($fp);
}

2 个答案:

答案 0 :(得分:2)

确保<?php 标记前面有

答案 1 :(得分:1)

检查所有标签是否关闭(打开和关闭)。 从php页面删除所有空白行。