php实现下载功能

php实现下载的功能,我用的是thinkphp框架,直接贴代码:

$map['id'] = I('get.id');
$file = M('document')->where($map)->getfield('file');
// 如果是纯PHP,这个得file记得用绝对路径
$file = realpath(C('UPLOAD_PATH')) . $file;
$length = filesize($file);
$showname =  ltrim(strrchr($file,'/'),'/');	 
header("Content-Description: File Transfer");
header('Content-type: application/octet-stream;charset=utf-8');
header('Content-Length:' . $length);
if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { //for IE
	header('Content-Disposition: attachment; filename="' . rawurlencode($showname) . '"');
} else {
	header('Content-Disposition: attachment; filename="' . $showname . '"');
}
readfile($file);
exit;