PHP code to implement text file download through header_PHP tutorial

WBOY
Release: 2016-07-21 15:34:48
Original
658 people have browsed it

This is the main issue discussed today. The instructions in the PHP help document about PHP triggering downloads through headers are relatively simple, and there are very few articles on this aspect on the Internet, and many articles cannot achieve the desired effect. Today I will also talk about this topic. If you feel that it is improved compared to some articles on the Internet, then I will be very satisfied.

From an accurate perspective, the PHP document is the most accurate, because it succinctly lists the three statements required to trigger the download of text files. Taking PDF as an example:

Copy code The code is as follows:

// We'll be outputting a PDF
header('Content-type: application /pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original .pdf
readfile('original.pdf');

These three sentences are correct, but some unforeseen problems are prone to occur during actual use. If you are a If you are very careful, you can easily avoid these problems. But I am not, so I encountered such a problem. Here I will briefly talk about my problem.

As for the first sentence, there should be nothing to say. It is necessary. Just change the type of the document. For example, if you are downloading a txt file, then change it to header('Content-type: application/txt' );, the second sentence doesn’t say much, just 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 has more problems. The readfile function means to read a file and then output it. The path of the file here needs to be a real file path. If it is an original.txt file under the downloads folder, it can be like this Write readfile('downloads/original.txt');, and if the submitted page will output text and other characters, then the downloaded file will be a mixed file of the original file original.txt and the text output by the submitted page. I lacked careful observation here. As soon as I saw something was wrong, I immediately checked the code, but did not find that the above text was what I needed. After discovering this part of the content, you may quickly think of how to solve this problem. , that is, turning off the output of the text content of the submitted page.

At this point, our problem is solved, thus achieving the effect of triggering the download dialog box when the text file link is clicked.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322333.htmlTechArticleThis is the main issue discussed today. The instructions in the PHP help document about PHP triggering downloads through headers are relatively simple, and there are very few articles about this on the Internet. There are many articles...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!