文件下载限速
<?php
$filePath = './test.zip';
$fp = fopen($filePath, "r");
$fileSize=filesize($filePath);
header("Content-type:application/octet-stream");
header("Accept-Ranges:bytes");
header("Accept-Length:".$fileSize);
header("Content-Disposition: attachment; filename=".basename($filePath));
ob_end_clean();
ob_implicit_flush();
header('X-Accel-Buffering: no');
$buffer=1024;
$bufferCount=0;
while(!feof($fp) && $fileSize - $bufferCount > 0){
$data = fread($fp, $buffer);
$bufferCount += $buffer;
echo $data;
sleep(1);
}
fclose($fp);
文件断点续传
<?php
$filePath = './test.txt';
$fp=fopen($filePath, "r");
$fileSize=filesize($filePath);
$buffer=5000;
$bufferCount=0;
header("Content-type:application/octet-stream");
header("Content-Disposition: attachment; filename=".basename($filePath));
if (!empty($_SERVER['HTTP_RANGE'])){
$range = explode('-',substr($_SERVER['HTTP_RANGE'],6));
fseek($fp,$range[0]);
header('HTTP/1.1 206 Partial Content');
header("Content-Range: bytes $range[0]-$fileSize/$fileSize");
header("content-length:".$fileSize-$range[0]);
}else{
header("Accept-Length:".$fileSize);
}
ob_end_clean();
ob_implicit_flush();
header('X-Accel-Buffering: no');
while(!feof($fp) && $fileSize - $bufferCount > 0){
$data = fread($fp, $buffer);
$bufferCount += $buffer;
echo $data;
sleep(1);
}
fclose($fp);
多线程下载
<?php
$filePath = '127.0.0.1';
$ch = curl_init();
$headerData = [
"Range: bytes=0-1"
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerData);
curl_setopt($ch, CURLOPT_URL, $filePath);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 7);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, 1);
$sContent = curl_exec($ch);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($sContent, 0, $headerSize);
curl_close($ch);
$headerArr = explode("\r\n", $header);
foreach ($headerArr as $item) {
$value = explode(':', $item);
if ($value[0] == 'Content-Range') {
$fileSize = explode('/',$value[1])[1];
break;
}
}
var_dump($fileSize);
$mh = curl_multi_init();
$count = 5;
$handle = [];
$data = [];
$fileData = ceil($fileSize / $count);
for ($i = 0; $i < $count; $i++) {
$ch = curl_init();
if ($fileData > ($fileSize-($i * $fileData))) {
$headerData = [
"Range:bytes=" . $i * $fileData . "-" . ($fileSize)
];
}else{
$headerData = [
"Range:bytes=" . $i * $fileData . "-" .(($i+1)*$fileData)
];
}
echo PHP_EOL;
curl_setopt($ch, CURLOPT_URL, $filePath);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerData);
curl_setopt($ch, CURLOPT_MAXREDIRS, 7);
curl_multi_add_handle($mh, $ch);
$handle[$i] = $ch;
}
$active = null;
do {
$mrc = curl_multi_exec($mh, $active);
} while ($active);
for ($i = 0; $i < $count; $i++) {
$data[$i] = curl_multi_getcontent($handle[$i]);
curl_multi_remove_handle($mh, $handle[$i]);
}
curl_multi_close($mh);
$file = implode('',$data);
$arr = explode('x',$file);
var_dump($data);
var_dump(count($arr));
var_dump($arr[count($arr)-2]);