您的位置:首页 > 编程语言 > PHP开发

【转】PHP读取服务器端文件提供弹出下载窗口

2014-03-10 15:25 429 查看
转载自:http://blogread.cn/it/article/4376?f=wb

有些文件需要经过身份验证以后才能下载,我们不容用户知道下载的地址,甚至文件不存放在web文件夹下,感觉是不是做起来比较难呢?用PHP几行就可以了。这是在PHP官方手册提供的例子。

<?php
$file = 'monkey.gif';

if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
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($file));
ob_clean();
flush();
readfile($file);
}
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: