首页 > 网站 > 建站经验 > 正文

PHP强制下载文件

2024-04-25 20:36:05
字体:
来源:转载
供稿:网友

有时我们不想让浏览器直接打开文件,如PDF文件,而是要直接下载文件,那么以下函数可以强制下载文件,函数中使用了application/octet-stream头类型。

function download($filename){

if ((isset($filename))&&(file_exists($filename))){

header("Content-length: ".filesize($filename));

header('Content-Type: application/octet-stream');

header('Content-Disposition: attachment; filename="' . $filename . '"');

readfile("$filename");

} else {

echo "Looks like file does not exist!";

}

}

使用方法如下:

download('/down/test_45f73e852.zip');

 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表