//ob_start();
for($i=0;$i<10000;$i++)
{
$content .="见鬼";
}
$fn= "down.txt";
$size = strlen($content);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: application/force-download");
//Force the download
header("Content-Disposition: attachment; filename=txt.txt;" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ". $size);
echo $content;
// flush();
// ob_clean();如果开启ob_start();则原样输出内容
如果不开启Warning: Cannot modify header information - headers already sent by (由于我需要下载的内容是直接从数据库中查询,所有不想把内容写入到文件,在下载文件。
在此求教直接下载方法。即把要下载的内容保存在变量中,然后下载
后面你说的不太明白,下载不是给用户么?最终不还是依然给用户存到文件中么?
ob_clean();
for($i=0;$i<10000;$i++)
{
$content .="见鬼";
}
$fn= "down.txt";
$size = strlen($content);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: application/force-download");
//Force the download
header("Content-Disposition: attachment; filename=txt.txt;" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ". $size);
echo $content;
// flush();
现在又内容 $CONTENT = "内容内容内容内容内容内容内容内容";不用写入到文件file.txt 中,就可以直接下载。
也就是用户在下载时,不必要先写入到文件中,然后再下载
ob_start();
for($i=0;$i<10000;$i++)
{
$content .="见鬼";
}
$fn= "down.txt";
$size = strlen($content);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: application/force-download");
//Force the download
header("Content-Disposition: attachment; filename=txt.txt;" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ". $size);ob_clean();
echo $content;
<?php
ob_start();
for($i=0;$i<10000;$i++)
{
$content .="见鬼";
}
$fn= "down.txt";
$size = strlen($content);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream"); // 这里修改了下文件类型
//Force the download
header("Content-Disposition: attachment; filename=txt.txt;" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ". $size);ob_clean();
echo $content;
汗啊,IE7 ff 360 都不可以下载,谢谢了,那我看看