首页 > 编程 > PHP > 正文

PHP强制下载文件

2019-11-10 17:31:14
字体:
来源:转载
供稿:网友

header() 函数向客户端发送原始的 HTTP 报头。进入指定页面后就弹出文件下载对话框,如图

代码如下:

function download_file($archivo, $downloadfilename = null) { if (file_exists($archivo)) { $downloadfilename = $downloadfilename !== null ? $downloadfilename : basename($archivo); header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . $downloadfilename); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, PRe-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($archivo)); ob_clean(); flush(); readfile($archivo); exit; } }


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