- Prompt to download
- Site http://bbs.it-home.org
- 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 ();
- }
- ?>
Copy CodePHP 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:
- PHP Header function Strong drop load
- Site bbs.it-home.org
- $filename = '/path/'. $_get[' file '. '. TXT '; File path
- Header ("Content-type:application/force-download");
- Header ("content-disposition:attachment; Filename= ". basename ($filename));
- ReadFile ($filename);
- ?>
Copy CodeDescription: 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. |