PHP forces the code to download a file of the specified type

Source: Internet
Author: User
Tags readfile
    1. Prompt to download
    2. Site http://bbs.it-home.org
    3. function DownloadFile ($file) {
    4. /*coded by Alessio delmonti*/
    5. $file _name = $file;
    6. $mime = ' application/force-download ';
    7. Header (' Pragma:public '); Required
    8. Header (' expires:0 '); No cache
    9. Header (' Cache-control:must-revalidate, post-check=0, pre-check=0 ');
    10. Header (' Cache-control:private ', false);
    11. Header (' Content-type: '. $mime);
    12. Header (' content-disposition:attachment; Filename= '. basename ($file _name). ');
    13. Header (' content-transfer-encoding:binary ');
    14. Header (' Connection:close ');
    15. ReadFile ($file _name); Push It out
    16. Exit ();
    17. }
    18. ?>
Copy Code

PHP downloads files Instead of hyperlinks, which can reduce hotlinking! Send the file to the browser for the browser to download.

Take the TXT type as an example

To avoid the TXT file in the browser directly open method, you can rename the TXT file to the browser does not know the file (such as RAR), so that because the browser does not recognize the RAR type files, can only let users download. The above method, sometimes very does not apply OH.

We use another method, the header information of the PHP file header to set the format of the document to achieve the purpose of click-Download. Example:

    1. PHP Header function Strong drop load
    2. Site bbs.it-home.org
    3. $filename = '/path/'. $_get[' file '. '. TXT '; File path
    4. Header ("Content-type:application/force-download");
    5. Header ("content-disposition:attachment; Filename= ". basename ($filename));
    6. ReadFile ($filename);
    7. ?>
Copy Code

Description: The first header function sets the value of Content-type to Application/force-download, and the second header function sets the file to be downloaded. Note that the filename here does not include the path of the file name, the value of filename will be clicked after the download pop-up dialog box file name, if the path, the pop-up dialog file name is unknown, and finally through the ReadFile function, the file stream output to the browser, Realized the download of TXT file.

  • 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.