Home  >  Article  >  Backend Development  >  Adobe flash player 9.0 download PHP forced download type implementation code

Adobe flash player 9.0 download PHP forced download type implementation code

WBOY
WBOYOriginal
2016-07-29 08:44:501653browse

Copy code The code is as follows:


function downloadFile($file){
/*Coded by Alessio Delmonti*/                                                                                                                        
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 the file instead of downloading the hyperlink, which can reduce hot links! Give the file to the browser and let the browser download it

Take the txt type as an example
Since modern browsers can already recognize the txt document format, if you only make a text link to the txt document, it will just open a new window to display the txt file after clicking it. Content does not achieve the purpose of click-to-download. Of course, the solution to this problem can also be to rename the txt file to a file that the browser does not recognize (such as rar). In this case, because the browser cannot recognize the rar type file, the user can only download it. Another way is to use code to set the format of the document through the header to achieve the purpose of click download.
PHP code is as follows:


Copy code The code is 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 The function sets the value of Content-Type to application/force-download;
The second header function sets the file to be downloaded. Note that filename here is a file name that does not include a path. The value of filename will be the file name in the dialog box that pops up after clicking download. If there is a path, the file name in the dialog box that pops up is unknown;
Finally, through the readfile function, The file stream is output to the browser, thus realizing the download of txt files.
The above introduces the implementation code of adobe flash player 9.0 download PHP forced download type, including the content of adobe flash player 9.0 download. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn