在项目有终于到一个问题,就是当我用a链接打开的是文件的时候大部分是可以下载的,在TXT的时候却是在浏览器中直接打开,于是在网上找了下下载TXT文件的方法
首先是看下效果图:
要实现的效果是:
这里用到的函数:<?php
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 "文件不存在!";
}
}
download('./now.txt');