金灿灿的朝晖,渐渐染红了东方的天际,高高的黄山主峰被灿烂的云霞染成一片绯红。太阳在朝霞的迎接中,露出了红彤彤的面庞,霎时,万道金光透过树梢给水面染上了一层胭脂红。
如果我们不想让浏览器直接打开服务器上PDF,txt文件,则直接将它们下载到本地,可以通过修改header头协议的方法进行实现。下面就来详细的说一说。
php实现文件下载的方法
下面提供一个自定义的php下载文件的函数,我们下载文件时直接调用此函数即可。
php文件下载函数
/**
* php 实现文件下载
*
* http://feiniaomy.com
*
* @param $filename 服务器上被下载文件的路径
* @return string
*/
function download($filename)
{
if ((isset($filename))&&(file_exists($filename))){
header("Content-length: ".filesize($filename));
header('Content-Type: application/octet-stream');
header("Accept-Ranges: bytes");
header('Content-Disposition: attachment; filename="' . $filename . '"');
readfile("$filename");
} else {
// return false;
echo "没有找到要下载的文件!";
}
}函数调用:
#下载图片文件
download('bb.png');
#下载txt文件
download('em.txt');
#下载压缩包
download('feiniaomy.zip'); 以上就是php如何实现文件下载的方法。而不语是一种成长,痛而不言是一种历练。更多关于php如何实现文件下载的方法请关注haodaima.com其它相关文章!




