PHP uses the range protocol to implement resumable Upload of output files. example _ PHP Tutorial

Source: Internet
Author: User
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...

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.