Adobe Flash Player 9.0 download implementation code for PHP mandatory download type

Source: Internet
Author: User
Tags readfile
Copy CodeThe code is as follows:


function DownloadFile ($file) {
/*coded by Alessio delmonti*/
$file _name = $file;
$mime = ' application/force-download ';
Header (' Pragma:public '); Required
Header (' expires:0 '); No cache
Header (' Cache-control:must-revalidate, post-check=0, pre-check=0 ');
Header (' Cache-control:private ', false);
Header (' Content-type: '. $mime);
Header (' content-disposition:attachment; Filename= '. basename ($file _name). ');
Header (' content-transfer-encoding:binary ');
Header (' Connection:close ');
ReadFile ($file _name); Push It out
Exit ();
}


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
Since the current browser can already recognize the TXT document format, if only to make a text link to the TXT document, click to open a new window to display the contents of the TXT file, and can not achieve the purpose of click to download. Of course, the solution to this problem can also be renamed TXT file to the browser does not know the file (such as RAR), so that the browser does not recognize the RAR type files, can only let users download. Another way is to use the code to format the document through the header to achieve the purpose of click to download.
The PHP code is as follows:

Copy the Code code 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 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, pop-up dialog box filename is unknown;
Finally through the ReadFile function, the file stream output to the browser, so that the implementation of the TXT file download.

The above describes the Adobe Flash Player 9.0 download PHP mandatory download type implementation code, including the Adobe Flash Player 9.0 download content, want to be interested in PHP tutorial friends helpful.

  • Related Article

    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.