Php enforces download type implementation code. Copy the code as follows: functiondownloadFile ($ file) {* CodedbyAlessioDelmonti * $ file_name $ file; $ mimeapplicationforce-download; header (Pragma: public); requir
The 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 leeching! Give the file to the browser for download
Take the txt type as an example
Because the current browser can recognize the txt document format, if only a text link is provided for the txt document, the content of the txt file is displayed in a new window after clicking it, click Download cannot be implemented. Of course, the solution to this problem can also be to rename the txt file as a file not recognized by the browser (such as rar). In this case, the browser cannot recognize rar files, you can only download the file. Another way is to use the code to set the document format through the header to achieve click download.
The PHP code is as follows:
The code is as follows:
$ Filename = '/path/'.w._getw.'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 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 is used to output the file stream to the browser, so as to download the txt file.
The http://www.bkjia.com/PHPjc/323300.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/323300.htmlTechArticle code is as follows: function downloadFile ($ file) {/* Coded by Alessio Delmonti */$ file_name = $ file; $ mime = 'application/force-download '; header ('pragma: public '); // requir...