PHP Support breakpoint Continuation, block download class, PHP support breakpoint Continuation _php Tutorial

Source: Internet
Author: User
Tags socket connect

PHP supports breakpoint continuation, block download class, PHP support breakpoint continuation


This article is for everyone to share PHP support breakpoint continuation, 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 for breakpoint download class */class Downloader {/* * * 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, $spee    D = 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 (CO mpatible; 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, $timeout);    Content length not exist if (! $content _length) {throw new Exception (' Content-length was 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 = PA    Rse_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 $headers * @ Param $timeout */static function download_content ($host, $port, $url _path, $save _file, $content _length, $range _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 $headers * @param $ Timeout * @return int */static function get_content_size ($host, $port, $url _path, & $headers, $timeout) {$req    Uest = 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 (' cont        Ent-length: ', $line);      $length = (int) trim ($length);    }} fclose ($fsocket);   return $length; }/** * Build header for Socket * * @param $action * @param $url _path * @param $headers * @param int $    Range_start * @return String */static function Build_header ($action, $url _path, & $headers, $range _start =-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 whole content of this article, I hope that everyone's study has helped.

http://www.bkjia.com/PHPjc/1125251.html www.bkjia.com true http://www.bkjia.com/PHPjc/1125251.html techarticle PHP Support breakpoint Continuation, block download class, PHP support Breakpoint continued this article is for everyone to share PHP support breakpoint continuation, block download class, for your reference, specific content as follows PHP/** ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.