<?php
if (isset($_GET['id']) && !empty($_GET['id'])) {
    
    $id = $_GET['id'];
    $aid = intval($id / 1000);
    $path = dirname(__FILE__)."/txt/".$aid."/".$id.".txt";
    #echo $path;
    #header("Location: //dl.fltxt.com/txt/".$aid."/".$id.".txt");
    down_file($path,$id);
    #exit;
} else {
    // 返回空白页面
    echo "";
}
function down_file($path, $filename){

	header("Content-type: application/x-octet-stream");
	header("Accept-Ranges: bytes");
	header("Content-Disposition: attachment; filename=".$filename.".txt");
    $file_size = filesize($path);
	header("Content-Length: ".$file_size);
	//nginx 利用 X-Accel-Redirect 直接定位成静态下载

	if(!empty($_SERVER['SERVER_SOFTWARE']) && preg_match('/nginx/is', $_SERVER['SERVER_SOFTWARE'])){
		header("X-Accel-Redirect: ".str_replace(dirname(__FILE__), '', $path));
		return true;
	}
	$fp = fopen($path,"rb");
	$buffer_size = 1024;
	$cur_pos = 0;
	while(!feof($fp) && $file_size - $cur_pos > $buffer_size){
		echo fread($fp,$buffer_size);
		ob_flush();
		flush();
		$cur_pos += $buffer_size;
	}
	echo fread($fp,$file_size-$cur_pos);
	ob_flush();
	flush();
	fclose($fp);
	return true;
	
}
?>