Example of php sending download of various types of files using headers

Source: Internet
Author: User
Tags php download
Example of php sending download of various types of files using headers

  1. Header ('content-type: application/image/pjpeg '); // output type
  2. Header ('content-Disposition: attachment; filename = "downloaded.jpg" '); // download the name displayed. Note the format.
  3. Readfile('my.jpg ');
  4. // Set the type of the file header to be sent, and a download box will pop up.
  5. // Download the my.jpg file on the server. the downloaded display and saved name are downloaded.jpg.
  6. ?>

The so-called implementation of text file download means that when we click a link to a text file, we do not open this file, but bring up a download dialog box for us to download. this is the main question 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. 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:

  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 ');
  7. ?>

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.

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, that is, to name your downloaded document. if it is a txt file, you can change it to header ('content-Disposition: attachment; filename=”downloaded.txt "'); the third sentence has many problems. The readfile function reads a file and outputs the file. here, the file contains a real File. if it is an original.txt file under the downloadsfolder, you can write readfile ('downloads/original.txt ') in this way. if the submitted page outputs text and other characters, the downloaded file is a hybrid file of the original original.txt file and the text output on 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.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.