Php force File Download

Source: Internet
Author: User
Because the current browser can recognize the file format in txt, if you just make a text link to the txt file, you may just open a new window to display the content of the txt file, click Download cannot be implemented. Wehavetodosomethingelse. of course, the solution to this problem can also be that you change your txt file to "> <LINKhref =" http:

Because the current browser can recognize the file format in txt, if you just make a text link to the txt file, you may just open a new window to display the content of the txt file, click Download cannot be implemented. We have to do something else.
Of course, the solution to this problem can also be that you change your txt file to a file that the browser does not know, such as rar. if you click this file, the browser will have to download the file if you do not know it.

The following File sets the header to set the document format for Click Download. you can click to download the file passed on the previous page.

$ Filename = "/somepath/". $ _ GET ['file']. ". txt"; // file name to be downloaded
 
Header ("Content-Type: application/force-download ");
Header ("Content-Disposition: attachment; filename =". basename ($ filename ));
Readfile ($ filename );
 
?>

First, set the Content-Type value to application/force-download and force download.

The second header function sets the file to be downloaded. Note that filename is a file name that does not contain a path, so use basename to filter out the path name. The value of this filename will be the file name in the pop-up dialog box after you click download.

The last step is readfile, which outputs the file stream to the browser, so that the txt file can be downloaded. Other types are similar.

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.