Home >Backend Development >PHP Tutorial >How to download text files using php header function

How to download text files using php header function

WBOY
WBOYOriginal
2016-07-25 09:00:351023browse
  1. header('Content-type: application/pdf');
  2. header('Content-Disposition: attachment; filename="downloaded.pdf"');
  3. readfile('original.pdf ');
  4. ?>
Copy code

Explanation of the above code: The first sentence is just to change the document type. For example, if you want to download a txt file, change it to header(‘Content-type: application/txt’);. The second sentence is to give your downloaded document a name. If it is a txt file, you can change it to header(‘Content-Disposition: attachment; filename="downloaded.txt”‘); The third sentence, the readfile function means to read a file and then output it. The path of the file here needs to be the real file path. If it is an original.txt file under the downloads folder, you can write readfile('downloads/original .txt');, and if the submitted page will output text and other characters, the downloaded file will be a mixed file of the original file original.txt and the text output by the submitted page.

With the above understanding, you can easily solve the problem of downloading txt files.

In addition, in order to facilitate understanding, it is recommended to read: PHP header Content-Type type summary. This article summarizes a very comprehensive PHP header content type.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn