function download_file($file = NULL, $file_name, $speed_limit = 1024, $resume = true, $send_errors = false){
//return:: 0 - ok \ 1 - $file is_null \ 2 - forbidden \ 3 - 404 Error
if(is_null($file)){
return 1;
}else{
// $file_name = basename($file);
$speed_limit = intval($speed_limit);
if($speed_limit<0) $speed_limit = 1024;
$running_time = 0;
$begin_time = time();
set_time_limit(300);
if(file_exists($file))
{
if( false !== ($file_hand = fopen($file, "rb")) )
{
$file_size = filesize($file);
$file_date = date("D, d M Y H:i:s T",filemtime($file));
if(preg_match("/bytes=(\d+)-/", $_SERVER["HTTP_RANGE"],$range) && $resume == true)
{
header("HTTP/1.1 206 Partial Content");
$offset = $file_size - intval($range[1]);
}else{
header("HTTP/1.1 200 OK");
$offset = 0;
}
$data_start = $offset;
$data_end = $file_size - 1;
$etag = md5($file.$file_size.$file_date);
fseek($file_hand, $data_start);
header("Content-Disposition: attachment; filename=".$file_name);
header("Last-Modified: ".$file_date);
header("ETag: \"".$etag."\"");
if($resume == true) header("Accept-Ranges: bytes");
header("Content-Length: ".($file_size-$data_start));
header("Content-Range: bytes ".$data_start."-".$data_end."/".$file_size);
header("Content-type: application/octet-stream");
while(!feof($file_hand) && (connection_status()==0))
{
print fread($file_hand,$speed_limit);
flush();
sleep(1);
$running_time = time() - $begin_time;
if($running_time>240)
{
set_time_limit(300);
$begin_time = time();
}
}
fclose ($file_hand);
return 0;
}
else
{
if($send_errors == true) header ("HTTP/1.0 403 Forbidden");
return 2;
}
}else{
if($send_errors == true) header("HTTP/1.0 404 Not Found");
return 3;
}
}
}