ITeye博客

php 实现文本下载

tanliwei · 2014-07-10 11:31 · php · header · Web前端 · Markdown

$file_name="a.txt"; 
//Windows PATH;
$file_path=dirname(__FILE__).'\\' . $file_name; 
//下载文件需要用到的头 
Header("Content-type: application/octet-stream"); 
Header("Content-Disposition: attachment; filename=".$file_name);
Header("Accept-Length: ".filesize($file_path)); 
//首先要判断给定的文件存在与否 
if(!file_exists($file_path)){ 
echo "没有该文件文件"; 
return ; 
}
readfile($file_path); 

 

设置好header后,(header的含义网上有)

需要做的一件事,是关闭迅雷的下载监听(或者其他类似的下载浏览器)


 

然后重新测试你的代码

参考:http://99webtools.com/php-force-file-download.php

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。