Heim  >  Artikel  >  Backend-Entwicklung  >  php header函数实现文本文件下载的方法

php header函数实现文本文件下载的方法

WBOY
WBOYOriginal
2016-07-25 09:00:35938Durchsuche
  1. header('Content-type: application/pdf');
  2. header('Content-Disposition: attachment; filename="downloaded.pdf"');
  3. readfile('original.pdf');
  4. ?>
复制代码

对以上代码的说明: 第一句,只要改一下文档的类型就行,例如是下载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内容类型。



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn