Home  >  Article  >  Backend Development  >  How to implement file download with php code

How to implement file download with php code

藏色散人
藏色散人Original
2020-08-05 10:29:382455browse

In PHP, you can download files through the header method. The syntax is "header('Content-type: application/pdf');", which is used to specify the type of downloaded document.

How to implement file download with php code

Recommendation: "PHP Video Tutorial"

PHP code for downloading text files through header

The so-called downloading of text files means that when we click on a link to a text file, instead of opening the file, a download dialog box pops up for us to download

This is today The main issues discussed. 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 downloading of text files. Taking PDF as an example:

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 can easily occur during actual use. If you are a very careful person, you can also easily solve the problem. It's easy to avoid these problems. But I am not, so I encountered such a problem. Here I will briefly talk about my problem.

There should be nothing to say about the first sentence. 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 I didn't 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.

The above is the detailed content of How to implement file download with php code. For more information, please follow other related articles on the PHP Chinese website!

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