PHP 文件分块下载实例

samool 发布暂无评论6136 阅读

AI摘要

本文介绍了一个PHP文件分块下载的实例,通过定义download_file函数实现分块下载功能。首先使用curl_get_with_head函数获取文件总长度和etag,然后根据文件大小和设定的分块数量计算每块的起始和结束位置,使用curl_get_with_body函数下载每一块内容,并使用file_put_contents函数将下载的内容写入本地文件。同时,文章还提供了curl_get_with_head和curl_get_with_body两个辅助函数的实现,用于发送带有HTTP头的GET请求和获取响应体。

调用方法:download_file($file);

public function download_file($file)
{
    $file = "https://www.inbeijing.org/people_snake.mp4";
    $range = "0-1";
    $response = $this->curl_get_with_head($file, array(
        "Range: bytes={$range}"
    ));
    $eTag = $response['etag'];
    $length = $response['length'];
    dd($length);
    $chunkCount = 100;
    $step = ceil($length / $chunkCount);
    for($i=0;$i<$chunkCount;$i++) {
        $start = $i * $step;
        $end = (($i+1) * $step) -1;
        if($end > $length) {
            $end = $length;
        }
        $range = $start. '-'. $end;
        $con = $this->curl_get_with_body('https://www.inbeijing.org/people_snake.mp4', $range);
        file_put_contents('c:\\snake.mp4', $con, FILE_APPEND);
        echo("总大小{$length},共计{$chunkCount}片,第{$i}片下载完成, range:". $range ."\n");
    }
}


//get请求
public function curl_get_with_head($url, $header)
{
    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "HEAD",
        CURLOPT_POSTFIELDS => "",
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => 2,
        //CURLINFO_HEADER_OUT => TRUE,    //获取请求头
        CURLOPT_HEADER => true, //获取响应头
        CURLOPT_NOBODY => true, //不需要响应正文
        CURLOPT_HTTPHEADER => $header,
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);
    $responseHeader = explode("\r\n", $response);
    $totalLength = $eTag = '';
    foreach($responseHeader as $response) {
        if($response){
            //获取文件总长度
            if(strpos($response, 'Content-Range:', 0) !== false) {
                $totalLength = explode(':', $response);
                $totalLength = array_pop($totalLength);
                $totalLength = explode('/', $totalLength);
                $totalLength = array_pop($totalLength);
            }
            //获取文件etag
            if(strpos($response, 'ETag:', 0) !== false) {
                $eTag = explode(':', $response);
                $eTag = array_pop($eTag);
                $eTag = trim(str_replace('"', '', $eTag));
            }
        }

    }

    if ($err) {
        return "cURL Error #:" . $err;
    } else {
        return ['length' => $totalLength, 'etag' => $eTag];
    }
}


//get请求获取body体
public function curl_get_with_body($url, $range)
{
    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_POSTFIELDS => "",
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => 2,
        CURLOPT_HTTPHEADER => array(
            "Range: bytes={$range}"
        ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);

    if ($err) {
        return "cURL Error #:" . $err;
    } else {
        return $response;
    }
}
  1. 01PHP2Shell 重大安全漏洞事件深度分析报告2026-10-23
  2. 02代码测试2020-07-30
  3. 03php json转换为数组调用的问题处理2020-05-05
  4. 04php检查URL是否有效2020-04-07
  5. 05PHP date() 函数可实现的功能列表2020-03-21
  6. 06php实现ipv6地址转换成数字INT类型存储数据库中2019-12-04
  7. 07PHP和MySQL中IPV4地址与INT的互相转换2019-12-04
  8. 08phpGrace 目录结构、命名规则2019-11-12
  9. 09phpGrace环境支持2019-11-12
  10. 10[PHP开源框架]phpGrace - 轻快的实力派!2019-11-12
COMMENTS / 一纸留言

读者留言 0 条

一纸留言,便是山水间的一次相逢。

还没有留言,欢迎写下第一段交流。

WRITE A COMMENT

写下你的留言