Home >Backend Development >PHP Tutorial >How to force image download in php by modifying header, _PHP tutorial

How to force image download in php by modifying header, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:01:06851browse

How to force image download in PHP by modifying the header,

This article describes the method of forcing image download in PHP by modifying the header. Share it with everyone for your reference. The specific implementation method is as follows:

function downloadFile($file){
 $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();
}

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/972638.htmlTechArticleHow to force image download in PHP by modifying the header. This article describes the method of forcing image download in PHP by modifying the header. Share it with everyone for your reference. The specific implementation method is as follows:...
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