php header函数实现文本文件下载的方法
我们先来看一个下载pdf文件的例子。
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('original.pdf');
?>
对以上代码的说明:
第一句,只要改一下文档的类型就行,例如是下载txt文件,那就改为header(‘Content-type: application/txt');,
第二句,就是为你的下载文档起一个名字,如果是txt文件的话,可以改为header(‘Content-Disposition: attachment; filename=”downloaded.txt”‘);
第三句,readfile这个函数的意思就是读取一个文件然后输出,这里文件的路径需要是真实的文件路径,如果是downloads文件夹下面的一个original.txt文件,可以这样写readfile(‘downloads/original.txt');,而如果提交的页面会输出文本等字符,那么下载到的文件会是原文件original.txt和提交的页面输出的文字的混合文件。
有了以上的理解,实现txt文件的下载,你也可以轻松解决了。
另外,为了方便理解,建议阅读下:php header Content-Type类型总结 这篇文章,汇总了非常全面的php header内容类型。
您可能感兴趣的文章:
php做下载文件的方法
php header头信息应用举例
php 强制下载文件实例代码
php header()函数的简单例子
php文件下载(防止中文文件名乱码)的示例代码
php导出word文档与excel表格文件
php header函数 文件下载时直接提示保存的代码
php文件下载代码(多浏览器兼容、支持中文文件名)
php实现文件下载实例代码
php中header函数的用法举例详解