支持断点续传的工具:PHP下载远程文件类(支持断点续传)来源: 发布时间:星期二, 2008年12月30日 浏览:152次 评论:0
1.功能:支持文件名,包括路径
$object->filename = $filename;//下载另存为 ![]() $object->download ![]() 3.源文件: <? ![]() var $data = null; var $data_len = 0; var $data_mod = 0; var $data_type = 0; var $data_section = 0; //section download var $sentSize=0; var $handler = var $use_resume = true; var $use_autoexit = false; var $use_auth = false; var $filename = null; var $mime = null; var $bufsize = 2048; var $seek_start = 0; var $seek_end = -1; var $totalsizeref = 0; var $bandwidth = 0; var $speed = 0; function initialize ![]() global $HTTP_SERVER_VARS; ![]() ![]() ![]() header('WWW-Authenticate: Basic realm="Please enter your username and password"'); header('HTTP/1.0 401 Unauthorized'); header('status: 401 Unauthorized'); ![]() ![]() ![]() } } ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() $range = explode('-',$seek_range); ![]() $this->seek_start = ![]() } ![]() ![]() ![]() ![]() $this->seek_start = 0; //header("HTTP/1.0 404 Bad Request"); //header("Status: 400 Bad Request"); //exit; // ![]() } ![]() $this->data_section = 1; } } ![]() $this->seek_start = 0; $this->seek_end = -1; } $this->sentSize=0; ![]() } function header($size,$seek_start=null,$seek_end=null) { header('Content-type: ' . $this->mime); header('Content-Disposition: attachment; filename="' . $this->filename . '"'); header('Last-Mod ![]() ![]() header("HTTP/1.0 206 Partial Content"); header("Status: 206 Partial Content"); header('Accept-Ranges: ![]() header("Content-Range: ![]() header("Content-Length: " . ($seek_end - $seek_start + 1)); } ![]() header("Content-Length: $size"); } } function download_ex($size) { ![]() ![]() ![]() ignore_user_abort(true); //Use seek end here ![]() ![]() $this->header($size,$seek,$this->seek_end); $this->data_mod = time ![]() ![]() } function download ![]() ![]() ![]() ![]() try { error_log("begin download\n", 3,"/usr/local/www/apache22/LOGS/apache22_php.err"); $seek = $this->seek_start; $speed = $this->speed; $bufsize = $this->bufsize; $packet = 1; //do some clean up @ob_end_clean ![]() $old_status = ignore_user_abort(true); @ ![]() $this->bandwidth = 0; $size = $this->data_len; ![]() ![]() $size = filesize($this->data); ![]() ![]() ![]() $res = fopen($this->data,'rb'); ![]() ![]() $this->header($size,$seek,$this->seek_end); //always use the last seek $size = $this->seek_end - $seek + 1; while (!(connection_aborted ![]() ![]() ![]() ![]() echo fread($res , $size); $this->bandwidth ![]() $this->sentSize ![]() } ![]() echo fread($res , $bufsize); $this->bandwidth ![]() $this->sentSize ![]() } $size -= $bufsize; flush ![]() ![]() sleep(1); $packet ![]() } } fclose($res); } ![]() ![]() ![]() ![]() { ![]() ![]() $this->data = substr($this->data , $seek , $this->seek_end - $seek + 1); ![]() ![]() ![]() $size = strlen($this->data); $this->header($this->data_len,$seek,$this->seek_end); while (!connection_aborted ![]() ![]() $this->bandwidth ![]() $this->sentSize ![]() } ![]() $this->bandwidth ![]() $this->sentSize ![]() } echo substr($this->data , 0 , $bufsize); $this->data = substr($this->data , $bufsize); $size -= $bufsize; flush ![]() ![]() sleep(1); $packet ![]() } } } ![]() ![]() ![]() //just send a redirect header header('location: ' . $this->data); } ![]() ![]() ![]() ![]() ![]() //restore old status ignore_user_abort($old_status); ![]() } catch(Exception $e) { error_log("cancel download\n".$e, 3,"/usr/local/www/apache22/LOGS/apache22_php.err"); } ![]() } function ![]() ![]() $this->data_len = 0; $this->data = $dir; $this->data_type = 0; $this->data_mod = filemtime($dir); $this->totalsizeref = filesize($dir); ![]() } ![]() ![]() } function ![]() ![]() ![]() ![]() $this->data = $data; $this->data_len = strlen($data); $this->data_type = 1; $this->data_mod = time ![]() ![]() } function ![]() $this->data = $data; $this->data_len = 0; $this->data_type = 2; ![]() } function ![]() $time = ![]() ![]() ![]() $this->data_mod = $time; } function _auth ![]() ![]() ![]() ![]() ![]() ![]() ![]() } ![]() ![]() } } ?> TAG: 下载 文件 PHP 断点 0
相关文章读者评论发表评论 |