Implementing Segmented Downloads in PHP (simple example)

Source: Internet
Author: User

The Http://127.0.0.1:20301/x.zip file is staged for download, and the service side is not required. Use the Range parameter to segment. Look at the code below:

<?php $fp = Fsockopen ("127.0.0.1", 20301, $errno, $errstr, 30); if (! $fp) {echo $errstr ($errno) <br/>/n ";} else {# first download $out =" Get/x.zip http/1.1/r/n "; $out. =" range:0-10/ r/n "; $out. = "host:127.0.0.1/r/n"; $out. = "connection:close/r/n/r/n"; Fwrite ($fp, $out); $start = false; while (!feof ($fp)) {$line = Fgets ($FP); if (! $start && ("/r/n" = = $line | | ' n ' = = $line)} {$start = true; continue} if ($start) {fwrite (fopen ('./a.zip ', ' WB '), $line);} # the second download $out = "Get/x.zip http/1.1/r/n"; $out. = "range:11-/r/n"; $out. = "host:127.0.0.1/r/n"; $out. = "connection:close/r/n/r/n"; Fwrite ($fp, $out); $start = false; while (!feof ($fp)) {$line = Fgets ($FP); if (! $start && ("/r/n" = = $line | | ' n ' = = $line)} {$start = true; continue} if ($start) {fwrite (fopen ('./a.zip ', ' WB '), $line);} Fclose ($FP); }

The header information is removed every time a request is written to a file. However, the test looks like the first paragraph can write header information. You can try it yourself, this is just a simple example.

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.