PHPackages                             yangwenqu/file\_chunk - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. yangwenqu/file\_chunk

ActiveLibrary[File &amp; Storage](/categories/file-storage)

yangwenqu/file\_chunk
=====================

大文件分片续传,上传、下载

v1.0.1(4y ago)4311MITPHPPHP &gt;=5.4

Since Jul 29Pushed 4y ago1 watchersCompare

[ Source](https://github.com/729343861/file_chunk)[ Packagist](https://packagist.org/packages/yangwenqu/file_chunk)[ RSS](/packages/yangwenqu-file-chunk/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

整合大文件分片上传,下载功能(如果需要在window环境运行,需要修改getChunk方法里的获取硬盘空间方法)

###### \* nginx配置client\_max\_body\_size;

[](#-nginx配置client_max_body_size)

###### \* php配置post\_max\_size、upload\_max\_filesize、memory\_limit、max\_input\_time

[](#-php配置post_max_sizeupload_max_filesizememory_limitmax_input_time)

分片上传
====

[](#分片上传)

- 支持多文件上传
- 支持乱序上传
- 支持异步上传
- 自定义文件存储目录
- 自定义临时分片存储目录
- 根据文件计算分片大小级文件名
- 服务器空间检测及预留
- 超时分片检测
- 失败分片获取
- 自定义文件名
- 断点续传配置
- 定时清理临时文件
- 非断点上传

断点下载
====

[](#断点下载)

- 自定义下载文件路径
- 自定义下载保存文件名
- 可配置非断点下载
- 可配置断点下载
- 可配置下载限速

上传示例代码
------

[](#上传示例代码)

```

//1.客户端上传文件前进行获取分片大小和唯一文件名
try {
    $file  = new Upload();
    $file->init([
        'redis'               =>[
            'scheme' => 'tcp',
            'host'   => '127.0.0.1',
            'port'   => 6379,
        ],
        'clear_interval_time' => 60,                                // 清理临时文件间隔，默认五分钟
        'disk_min_size'       => 20    // 服务器空间最低剩余空间，超过此空间将不接收分片
    ]);
    $res = $file->getChunk(10 * 1024 * 1024 ,'a.b.c.exe');
    if(!$res){
        echo "服务器空间不足,请联系客服扩容";
    }
    echo $res;  // { ["chunk_size"]=> float(524288) ["file_name"]=> string(42) "27ad9ac14aa35137552ab038e038a6b8-a.b.c.exe" }

}catch (Exception $exception){

    var_dump($exception->getMessage());
    var_dump($exception->getTraceAsString());
}

//2.客户端上传每个分片前请求接口来获取当前文件是否超时,之前的分片是否被清理,如果被请求则拒绝处理。返回客户端错误码，让客户端不再续传剩余分片
$file  = new Upload();
$file->init([
    'redis'               =>
        'scheme' => 'tcp',
        'host'   => '127.0.0.1',
        'port'   => 6379,
    ],
    'clear_interval_time' => 60,                                // 清理临时文件间隔，默认五分钟
]);
$is_clear = $file->check('27ad9ac14aa35137552ab038e038a6b8-a.b.c.exe');
echo $is_clear;

//3.进行接收分片处理
$param = $_REQUEST;
$res   = $file->upload([
    'redis'               =>[
        'scheme' => 'tcp',
        'host'   => '127.0.0.1',
        'port'   => 6379,
    ],                                                          // predis的配置
    'tmp_name'            => $_FILES['file']['tmp_name'],       // 文件内容
    'now_package_num'     => $param['blob_num'],                // 当前文件包数量
    'total_package_num'   => $param['total_blob_num'],          // 文件包总量
    'file_name'           => $param['file_name'],               // 唯一文件名称
    'file_path'           => './upload',                        // 文件存放路径
    'clear_interval_time' => 60,                                // 清理临时文件间隔，默认五分钟
    'is_continuingly'     => true,                              // 是否断点续传，默认为true
    'tmp_file_chunk'      => '/tmp/file_chunk'                  // 临时分片存放目录
]);
if (is_string($res)){
    return "文件合并成功";
}else{
    return "当前分片上传成功,当前分片编号是:{$res}";
}

//4.定时器执行清理碎片
$file  = new Upload();
$file->init([
    'redis'               =>[
        'scheme' => 'tcp',
        'host'   => '127.0.0.1',
        'port'   => 6379,
    ],
    'clear_interval_time' => 60,                                // 清理临时文件间隔，默认五分钟
    'tmp_file_chunk'      => '/tmp/file_chunk'                  // 临时分片存放目录
]);
$file->claerChunk();

//5.获取某个文件缺少的分片（用于在上传周期中进行补传分片）
$file  = new Upload();
$file->init([
    'redis'               =>[
        'scheme' => 'tcp',
        'host'   => '127.0.0.1',
        'port'   => 6379,
    ],
]);
$file->getFailChunk('27ad9ac14aa35137552ab038e038a6b8-a.b.c.exe');

```

下载示例代码
------

[](#下载示例代码)

注意：在下载http请求头中加入Range字段 Range: bytes=start-end \[表示从start读取，一直读取到end位置,第一次请求这里可以1-2000,第二次根据响应中的文件总字节自行计算每次分片下载的字节范围\]

```

$path     = './static/CentOS-7-x86_64-Everything-2009.iso';  //需要下载的文件目录+文件名
$filename = 'CentOS2009.iso';                                //下载保存的文件名
$file     = new Download();
$file->setSpeed(2)->download($path, $filename,true);         //限速2MB

```

响应示例
----

[](#响应示例)

```

HTTP/1.1 206 Partial Content     //断点续传http响应码为206
content-length=106786028         //剩余字节
content-range=bytes 2000070-106786027/106786028   //正在下载的字节范围 / 文件总字节
content-type=application/octet-stream    //mime类型:octet-stream

```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1745d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/289f9c06190ed260b0224502ec440ec7b02b000a402476ec70983f7f6507f0e9?d=identicon)[729343861](/maintainers/729343861)

---

Top Contributors

[![729343861](https://avatars.githubusercontent.com/u/35519825?v=4)](https://github.com/729343861 "729343861 (11 commits)")

### Embed Badge

![Health badge](/badges/yangwenqu-file-chunk/health.svg)

```
[![Health](https://phpackages.com/badges/yangwenqu-file-chunk/health.svg)](https://phpackages.com/packages/yangwenqu-file-chunk)
```

###  Alternatives

[knplabs/gaufrette

PHP library that provides a filesystem abstraction layer

2.5k39.8M123](/packages/knplabs-gaufrette)[ankitpokhrel/tus-php

A pure PHP server and client for the tus resumable upload protocol v1.0.0

1.5k7.3M22](/packages/ankitpokhrel-tus-php)[google/cloud-storage

Cloud Storage Client for PHP

34390.8M123](/packages/google-cloud-storage)[illuminate/filesystem

The Illuminate Filesystem package.

15261.6M2.6k](/packages/illuminate-filesystem)[superbalist/flysystem-google-storage

Flysystem adapter for Google Cloud Storage

26320.6M30](/packages/superbalist-flysystem-google-storage)[creocoder/yii2-flysystem

The flysystem extension for the Yii framework

2931.7M61](/packages/creocoder-yii2-flysystem)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
