Php forcibly downloads the code of a specified type of file

Source: Internet
Author: User
Php forcibly downloads the code of a specified type of file

  1. // Download prompt
  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. ?>

Php downloads files instead of hyperlinks, which can reduce leeching! Send the file to the browser for download.

Take the txt type as an example

To avoid the direct opening of txt files in the browser, you can rename the txt file as a file (such as rar) that the browser does not know. in this case, the browser cannot recognize rar files, you can only download the file. The above method is sometimes not applicable.

We use another method to set the document format through the header information of the php file to achieve the purpose of clicking download. Example:

  1. // Download the php header function
  2. // Site bbs.it-home.org
  3. $ Filename = '/path/'.w._getw.'file'{.'.txt'; // file path
  4. Header ("Content-Type: application/force-download ");
  5. Header ("Content-Disposition: attachment; filename =". basename ($ filename ));
  6. Readfile ($ filename );
  7. ?>

Note: the first header function sets the Content-Type value to application/force-download; the second header function sets the file to be downloaded. Note that the filename here is a file name that does not contain a path. The value of filename will be the file name in the pop-up dialog box after you click Download. if there is a path, the file name in the pop-up dialog box is unknown; finally, the readfile function outputs the file stream to the browser to download the 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.