---
title: "php 实现文本下载"
author: "tanliwei"
date: "2014-07-10T11:31:48Z"
tags: []
categories: ["Web前端"]
slug: "php"
draft: false
lastmod: "2014-07-10T11:31:48Z"
id: 2090293
url: "https://tanliwei.iteye.com/blog/2090293"
---

```
$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://dl2.iteye.com/upload/attachment/0098/9638/47be5b6c-6873-38fb-a027-6532824c4f28.jpg)  
 

然后重新测试你的代码

参考：[http://99webtools.com/php-force-file-download.php](http://99webtools.com/php-force-file-download.php)