PHP uses the range protocol to implement the code instance for resumable Upload of output files. Range protocol purpose: it is generally used for resumable data transfer, but the actual user is very large. for example, if your webserver needs to output a large file, you can use range to output data in segments to ease
Range protocol purpose:It is usually used for resumable data transfer, but the actual user is very large. for example, if your web server needs to output a large file, range can be used for multipart output to relieve pressure. At the same time, you can buffer downloads when providing music and video services. if you disable it halfway, you can save network bandwidth.
<? Php // file name $ filename = $ _ GET ['filename']; // file path $ location = 'media /'. $ filename; // suffix $ extension = substr (strrchr ($ filename ,'. '), 1); if ($ extension = "mp3") {$ mimeType = "audio/mpeg";} else if ($ extension = "ogg ") {$ mimeType = "audio/ogg";} if (! File_exists ($ location) {header ("HTTP/1.1 404 Not Found"); return;} $ size = filesize ($ location); $ time = date ('R ', filemtime ($ location); $ fm = @ fopen ($ location, 'RB'); if (! $ Fm) {header ("HTTP/1.1 505 Internal server error"); return ;}$ begin = 0; $ end = $ size-1; if (isset ($ _ SERVER ['http _ range']) {if (preg_match ('/bytes = \ h * (\ d +)-(\ d *) [\ D. *]? /I ', $ _ SERVER ['http _ range'], $ matches) {// read the file, starting node $ begin = intval ($ matches [1]); // read the file and end the node if (! Empty ($ matches [2]) {$ end = intval ($ matches [2]) ;}} if (isset ($ _ SERVER ['http _ range']) {header ('http/1.1 206 Partial content ');} else {header ('http/1.1 200 OK ');} header ("Content-Type: $ mimeType"); header ('cache-Control: public, must-revalidate, max-age = 0'); header ('pragma: no-cache'); header ('Accept-Ranges: bytes '); header ('content-Length :'. ($ end-$ begin) + 1); if (Isset ($ _ SERVER ['http _ range']) {header ("Content-RANGE: bytes $ begin-$ end/$ size ");} header ("Content-Disposition: inline; filename = $ filename"); header ("Content-Transfer-Encoding: binary"); header ("Last-Modified: $ time "); $ cur = $ begin; // position pointer fseek ($ fm, $ begin, 0); while (! Feof ($ fm) & $ cur <= $ end & (connection_status () = 0) {print fread ($ fm, min (1024*16, ($ end-$ cur) + 1); $ cur ++ = 1024*16 ;}
Range protocol official documentation: Http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
Resume is generally used for resumable data transfer, but the actual user is very large. for example, if your web server needs to output a large file, range can be used to output the file in segments to ease...