PHP downloads text files using headers. The so-called implementation of text file download means that when we click a link to a text file, instead of opening the file, a download dialog box pops up for us to download, this is the so-called implementation of text file download. that is to say, when we click a link to a text file, instead of opening the file, a download dialog box is displayed for us to download, this is the main issue discussed today. The instructions in the PHP help document on triggering PHP download through the header are relatively simple, and there are few articles on this page on the internet. many articles cannot achieve the desired effect. Today, I will also talk about this topic. if you feel better than some articles on the Internet, I will be satisfied. 6} g> o % P
From an accurate point of view, the PHP document is the most accurate, because it provides a concise list of the three statements required for triggering download of text files. take PDF as an example: g < // We'll be outputting a PDF lh? Cuj Qh
Header ('content-type: application/pdf '); I [xR. p
// It will be called downloaded.pdf rWf {= Pk'
Header ('content-Disposition: attachment; filename = "downloaded.pdf" '); HOAw: * Z {>
// The PDF source is in originalw.j4qsc \ 1
Readfile('original}'); 0 pc * r4R @
<| These three statements are correct, but some unexpected problems may easily occur during actual use. if you are a very careful person, these problems can also be easily avoided. I am not, so I have encountered such a problem. here I will give a brief description of my problem. 1xraa7
For the first sentence, it should be unnecessary. you only need to change the document type. for example, to download a txt file, change it to header ('content-type: application/txt '); the second sentence is nothing to mention. it is to name your downloaded document. if it is a txt file, you can change it to header ('content-Disposition: attachment; in this way, you can write readfile ('downloads/original.txt'character, and if the page to be submitted is output, the downloaded file will be a hybrid file of the original file original.txt and the text output from the submitted page. I am missing a careful observation here. I checked the code immediately after reading the following error. I didn't find that the above text is what I needed. I found this part of content, you may soon think about how to solve this problem, that is, to close the output of the text content on the submitted page. I-z7 I; I
. FRO | }}
Wr # r? DhI
By now, the problem is solved, and the download dialog box is triggered when the text file link is clicked.
...