您的位置:首页 > 编程语言 > PHP开发

php生成txt.word.xls直接下载代码

2011-05-09 09:15 411 查看
php如何利用header函数实现点击特殊文件连接后提示下载保存解决方案
相信很多人在实现文件下载功能时会遇到这种情况,本意是点击下载连接提示保存对话框,事情往往事与愿违,例如TXT、图像文件JPG、GIF、PDF等这样的文件会直接在浏览器中打开文件内容,而不是提示保存。通过下面的方法可以解决这个问题。
<?php
if (sExport == "txt" ){女装品牌排行榜
header("Content-Type: application/force-download");//关键之一,提示下载(如:header("Content-Type:text/html");可能直接打开?)
header("Content-Disposition: attachment; filename=".$TxtName);//实际的文件名
//include_once("XXX.php"); //php代码(你把原来的php复制一份,去掉输出html标签)。
}
?>
//如果是xsl,word等则更简单,只需直接在原来的php文件开头加上下面几句:
//(因为word能直接编辑html标签)
<?php
if(sExport == "excel"){
header("ContentType = application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=".$oTitle.".xls");
}
if(sExport == "word"){
echo
("<div align='center'><font
size=+2>收文登记簿</font><hr size=3 align='center'
width=360></div><br>");
header("ContentType = application/vnd.ms-word");//或试试 application/msword
header("Content-Disposition: attachment; filename=".$oTitle.".doc");
}
if( sExport == "csv"){
header("ContentType = application/csv"); //图像image/jpeg",或image/*"
header("Content-Disposition: attachment; filename=".$oTitle.".csv");
}
//.....输出原来的php代码

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: