Home >Backend Development >PHP Tutorial >How to download text files using php header function
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. |