|
一些诸如 mp3 类型的文件,通常会在客户端浏览器中直接被播放或使用。如果你希望它们强制被下载,也没问题。本文介绍的这段代码,可以帮你实现。
代码如下:
<?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();
}
?>登录后复制 有关php header信息的相关内容,请参考:php头部文件(header)信息。 您可能感兴趣的文章: php强制文件下载(避免文件或图片直接在浏览器中打开) php 强制文件下载的一段代码 php强制下载指定类型文件的代码 php 强制文件下载的实现代码一例 php强制性文件下载的函数 |
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号