hamed/php-chunk-file-upload

一个用于处理分块文件上传的 PHP 包。

1.2.3 2021-12-25 09:35 UTC

This package is auto-updated.

Last update: 2024-09-25 15:39:58 UTC


README

一个轻量级的 PHP 包,用于处理分块文件上传。与几乎所有版本的 Laravel 兼容。

安装

您可以使用 composer 简单地安装此包。

composer require hamed/php-chunk-file-upload

https://packagist.org.cn/packages/hamed/php-chunk-file-upload

使用方法

要使用此包,首先创建一个 Uploader 类的实例,然后运行 "Upload" 方法。

构造函数

构造函数接受两个任意键和值来配置包。

  • max_upload => 允许上传的最大字节数;如果用户尝试上传超过 max_upload (max_upload < file_size) 的文件,将返回错误。
  • chunk_folder => 所有分块文件将放置的文件夹。

上传

Upload 方法接受以下键和值

  • chunk_number => 分块文件的编号。必须按顺序增加每个分块文件。
  • chunks_count => 分块文件的总数。
  • chunk_path => 传入的分块文件的路径
  • file_size => 文件的大小。
  • file_name => 最终文件的名称
方法返回值

Upload 方法返回以下内容

  • 如果不是最后一个分块文件上传成功,它将返回一个代表上传百分比的 float 值。
  • 如果所有分块都上传成功,它将返回一个代表最终文件路径的 string 值。

示例

以下是一个可恢复 JavaScript 库请求的示例。

use Hamed\ChunkFile\Uploader;

...

try {
   $file_path = (new Uploader())->Upload([
      'chunk_path' => $request->file('file')->getRealPath(),
      'file_name' => $request->resumableFilename,
      'chunk_number' => $request->resumableChunkNumber,
      'chunks_count' => $request->resumableTotalChunks,
      'file_size' => $request->resumableTotalSize,
      'max_upload' => 5 * pow(10, 6),
      'errors' => ['max_upload' => "Low Space"],
   ]);
} catch (Exception $exception) {
   return response($exception->getMessage(), 403);
}

if (is_string($file_path)) echo("File path: $file_path"); // Upload has finished. You can move the file.
else echo("Progress: {$file_path}%") // Chunk has been uploaded. Print the percentage of the upload ;)