Home  >  Article  >  Backend Development  >  Implementation code for php to force download of mp3 files

Implementation code for php to force download of mp3 files

WBOY
WBOYOriginal
2016-07-25 09:00:001479browse
Some files such as mp3 are usually played or used directly in the client browser. If you want them to be forced to download, that's no problem. The code introduced in this article can help you implement it.

The code is as follows:

<?php
/**
 * 强制文件下载
 * by http://bbs.it-home.org
*/
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(); 
}
?>

For related content about php header information, please refer to: php header file (header) information. Articles you may be interested in: php forces file download (avoid files or images being opened directly in the browser) php code to force file download php code to force download of specified type of file An example of php forced file download implementation code php mandatory file download function



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