PHP to implement browser Click to download the TXT document method detailed _php skills

Source: Internet
Author: User
Tags rar readfile

Because now the browser has been able to identify TXT document format, if only to do a text link txt document, click on just open a new window to display TXT file content, and can not achieve the purpose of the download. Of course, the solution to this problem can also be TXT file renamed as a browser do not know the file (such as RAR), so that, because the browser can not identify the type of RAR files, can only allow users to download. Another option is to use the code to format the document through the header to achieve the purpose of clicking the download.
The PHP code is as follows:
===========================================================
$filename = '/path/'. $_get[' file '. TXT '; File path
Header ("Content-type:application/force-download");
Header ("content-disposition:attachment; Filename= ". basename ($filename));
ReadFile ($filename);
===========================================================
Brief description:
The first header function sets the value of Content-type to Application/force-download;
The second header function sets the file to be downloaded. Note that the filename here is not including the path of the file name, filename value in the future is to click the Download pop-up dialog box inside the file name, if with the Path, pop-up dialog box filename is unknown;
Finally, through the ReadFile function, the file stream output to the browser, so that the TXT file to achieve the download.

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.