對http://127.0.0.1:20301/x.zip檔案進行分段下載,不需要編寫服務端。使用 Range 這個參數來進行分段。下面看下代碼:
<?php $fp = fsockopen("127.0.0.1", 20301, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />/n"; } else { # 第一段下載 $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); } } # 第二段下載 $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); }
每次請求寫入檔案的時候都去除了頭資訊。不過測試好像第一段可以寫入頭資訊。可以自己多試試,這個只是個簡單例子而已。