用PHP实现 HTTP断点续传、分块下载文件(Socket)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | <?php /** * 支持断点下载的类 */ 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 , $speed = 10240, $headers = array (), $timeout = 10) { $url_info = self:: parse_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 , $timeout ); // content length not exist if (! $content_length ) { throw new Exception( 'Content-Length is 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::download_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 $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 ) { $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:: parse_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 ( 'content-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 ; } } ?> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)