This article is for you to share PHP support breakpoint, block download class, for your reference, the specific content as follows
<?php/** * User:djunny * date:2016-04-29 * time:17:18 * mail:199962760@qq.com * Support Breakpoint Download class */class Downloa
der {/** * download file to local path * * @param $url * @param $save _file * @param int $speed * @param array $headers * @param int $timeout * @return BOOL * @throws Exception */static function Get (
$url, $save _file, $speed = 10240, $headers = Array (), $timeout = ten) {$url _info = self::p arse_url ($url);
if (! $url _info[' host ']) {throw new Exception (' URL is Invalid '); }//default header $def _headers = Array (' Accept ' => ' */* ', ' user-agent ' => ') mozilla/4. 0 (compatible; MSIE 8.0; Windows NT 5.1; trident/4.0) ', ' accept-encoding ' => ' gzip, deflate ', ' host ' => $url _info[' host '], ' Connection
' => ' close ', ' accept-language ' => ' ZH-CN ',);
Merge Heade $headers = Array_merge ($def _headers, $headers); Get content LenGth $content _length = self::get_content_size ($url _info[' host '), $url _info[' Port ', $url _info[' request '], $headers, $t
Imeout);
Content length not exist if (! $content _length) {throw new Exception (' content-length are not Exists '); }//Get exists length $exists _length = Is_file ($save _file)?
FileSize ($save _file): 0; Get TMP data file $data _file = $save _file.
'. Data '; Get tmp data $exists _data = Is_file ($data _file)?
Json_decode (file_get_contents ($data _file), 1): Array ();
Check file is valid if ($exists _length = = $content _length) {$exists _data && @unlink ($data _file);
return true;
}//Check file is expire if ($exists _data[' length ']!= $content _length | | $exists _length > $content _length) {
$exists _data = Array (' length ' => $content _length);
}//write exists Data file_put_contents ($data _file, Json_encode ($exists _data)); try {$download _sTatus = self::d ownload_content ($url _info[' host '), $url _info[' Port ', $url _info[' request '], $save _file, $content _
Length, $exists _length, $speed, $headers, $timeout);
if ($download _status) {@unlink ($data _file);
} catch (Exception $e) {throw new Exception ($e->getmessage ());
return true; /** * Parse URL * * @param $url * @return bool|mixed/static function Parse_url ($url) {$url _
info = Parse_url ($url);
if (! $url _info[' host ']) {return false; $url _info[' port ' = $url _info[' Port '?
$url _info[' host ']: 80; $url _info[' request '] = $url _info[' path '. ($url _info[' query ')? '?' .
$url _info[' query ']: ";
return $url _info; }/** * Download content by chunk * * @param $host * @param $port * @param $url _path * @param RS * @param $timeout/static function download_content ($host, $port, $url _path, $save _file, $content _length, $ra Nge_start, $speed, & $hEaders, $timeout) {$request = Self::build_header (' Get ', $url _path, $headers, $range _start);
$fsocket = @fsockopen ($host, $port, $errno, $errstr, $timeout);
Stream_set_blocking ($fsocket, TRUE);
Stream_set_timeout ($fsocket, $timeout);
Fwrite ($fsocket, $request);
$status = Stream_get_meta_data ($fsocket);
if ($status [' timed_out ']) {throw new Exception (' Socket Connect Timeout ');
} $is _header_end = 0;
$total _size = $range _start;
$file _fp = fopen ($save _file, ' A + ');
while (!feof ($fsocket)) {if (! $is _header_end) {$line = @fgets ($fsocket);
if (In_array ($line, Array ("\ n", "\ r \ n")) {$is _header_end = 1;
} continue;
} $RESP = Fread ($fsocket, $speed);
$read _length = strlen ($RESP);
if ($resp = = False | | $content _length < $total _size + $read _length) {fclose ($fsocket);
Fclose ($file _fp); throw new Exception (' Socket I/O Error Or File was ChAnged ');
$total _size + = $read _length;
Fputs ($file _fp, $RESP);
Check file End if ($content _length = = $total _size) {break;
Sleep (1);
for Test//break;
} fclose ($fsocket);
Fclose ($file _fp);
return true; /** * Get Content length * * @param $host * @param $port * @param $url _path * @param * @param $timeout * @return int/static function get_content_size ($host, $port, $url _path, & $headers, $timeou
T) {$request = Self::build_header (' head ', $url _path, $headers);
$fsocket = @fsockopen ($host, $port, $errno, $errstr, $timeout);
Stream_set_blocking ($fsocket, TRUE);
Stream_set_timeout ($fsocket, $timeout);
Fwrite ($fsocket, $request);
$status = Stream_get_meta_data ($fsocket);
$length = 0;
if ($status [' timed_out ']) {return 0;
while (!feof ($fsocket)) {$line = @fgets ($fsocket); if (In_array ($line, ArraY ("\ n", "\ r \ n"))} {break;
} $line = Strtolower ($line);
Get location if (substr ($line, 0, 9) = = ' Location: ') {$location = Trim (substr ($line, 9));
$url _info = self::p arse_url ($location);
if (! $url _info[' host ']) {return 0;
} fclose ($fsocket);
Return self::get_content_size ($url _info[' host '), $url _info[' Port ', $url _info[' request '], $headers, $timeout); }//Get content length if (Strpos ($line, ' content-length: ')!== false) {list (, $length) = Explode (' C
Ontent-length: ', $line);
$length = (int) trim ($length);
} fclose ($fsocket);
return $length; /** * Build Header for Socket * * @param $action * @param $url _path * @param $headers * @par AM int $range _start * @return string/static function Build_header ($action, $url _path, & $headers, $range _sta RT =-1) {$out = $action. ' {$url _path} http/1.0\r\n"; foreach ($headers as $hkey => $hval) {$out. = $hkey. ': ' . $hval.
"\ r \ n";
} if ($range _start >-1) {$out. = "accept-ranges:bytes\r\n";
$out. = "range:bytes={$range _start}-\r\n";
$out. = "\ r \ n";
return $out; #use Age/* Try {if (Downloader::get (' Http://dzs.aqtxt.com/files/11/23636/201604230358308081.rar ', ' Test.rar '))
{//todo echo ' Download Succ '; } catch (Exception $e) {echo ' Download Failed ';} */?>
The above is the entire content of this article, I hope to help you learn.