PHP forced download type implementation code_PHP tutorial

WBOY
Release: 2016-07-21 15:30:15
Original
1125 people have browsed it

Copy code 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 the file instead of downloading the hyperlink, which can reduce the number of hot links! Give the file to the browser and let the browser download it

Take the txt type as an example

Since the current browser can already recognize the txt document format, if you only want to make a text link to the txt document, click After that, it just opens a new window to display the content of the txt file, and the purpose of clicking to download cannot be achieved. 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.
The 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 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, the readfile function is used , output the file stream to the browser, thus realizing the download of txt file.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323300.htmlTechArticleCopy the code as follows: function downloadFile($file){ /*Coded by Alessio Delmonti*/ $file_name = $ file; $mime = 'application/force-download'; header('Pragma: public'); // require...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!