Home > Article > Backend Development > Implementation code for php to force download of mp3 files
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 |