Home >Backend Development >PHP Tutorial >Examples of php using headers to send various types of files for download

Examples of php using headers to send various types of files for download

WBOY
WBOYOriginal
2016-07-25 09:04:38735browse
  1. header('Content-type: application/image/pjpeg');//Output type
  2. header('Content-Disposition: attachment; filename="downloaded.jpg"'); //Download the displayed name, pay attention to the format
  3. readfile('my.jpg');
  4. // And output this file with the type set by the previous header sending information, so a download box will pop up
  5. // That is, put it on the server My.jpg is downloaded, and the download display and saved name are downloaded.jpg by default
  6. ?>
Copy code

The so-called downloading of text files means that when we click on a link to a text file, it does not open it Instead of this file, a download dialog box pops up for us to download. 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. 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:

  1. // We'll be outputting a PDF
  2. header('Content-type: application/pdf');
  3. // It will be called downloaded.pdf
  4. header('Content- Disposition: attachment; filename="downloaded.pdf"');
  5. // The PDF source is in original.pdf
  6. readfile('original.pdf');
  7. ?>
Copy the code

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

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’s 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 written like this 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. When 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. The problem is to turn off the output of the text content of the page submitted to.

The problem is solved, and the download dialog box is triggered when the text file link is clicked.



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