lkt/file-upload

能够处理大文件/分块/多文件上传的文件上传库

1.0.2 2023-01-23 18:42 UTC

This package is auto-updated.

Last update: 2024-09-24 14:11:18 UTC


README

Build Status

支持分块上传的PHP文件上传库。从jQuery-File-Upload的进程脚本中采用,旨在与该JavaScript插件一起使用,与普通表单一起使用,并可以嵌入到任何应用程序/架构中。

安装

此软件包可通过Composer获取

{
  "require": {
    "gargron/fileupload": "~1.4.0"
  }
}

要求

  • 确保已启用PHP扩展"php_fileinfo";

  • 您的php.ini必须包含以下指令

file_uploads = On

状态

单元测试套件涵盖了简单的上传,"在我的机器上工作",您欢迎贡献力量。

您可以通过grep源代码中的TODO来找到您可以帮助完成的事情。

用法

// Simple validation (max file size 2MB and only two allowed mime types)
$validator = new Validator\Simple('2M', ['image/png', 'image/jpg']);

// Simple path resolver, where uploads will be put
$pathresolver = new PathResolver\Simple('/my/uploads/dir');

// The machine's filesystem
$filesystem = new FileSystem\Simple();

// FileUploader itself
$fileupload = new \FileUpload($_FILES['files'], $_SERVER);

// Adding it all together. Note that you can use multiple validators or none at all
$fileupload->setPathResolver($pathresolver);
$fileupload->setFileSystem($filesystem);
$fileupload->addValidator($validator);

// Doing the deed
list($files, $headers) = $fileupload->processAll();

// Outputting it, for example like this
foreach($headers as $header => $value) {
    header($header . ': ' . $value);
}

echo json_encode(['files' => $files]);

foreach($files as $file){
    //Remeber to check if the upload was completed
    if ($file->completed) {
        echo $file->getRealPath();
        
        // Call any method on an SplFileInfo instance
        var_dump($file->isFile());
    }
}

通过工厂的替代用法

$factory = new FileUploadFactory(
    new PathResolver\Simple('/my/uploads/dir'), 
    new FileSystem\Simple(), 
    [
        new Validator\MimeTypeValidator(['image/png', 'image/jpg']),
        new Validator\SizeValidator('3M', '1M') 
        // etc
    ]
);

$instance = $factory->create($_FILES['files'], $_SERVER);

验证器

目前,FileUpload包含4个验证器

  • 简单
// Simple validation (max file size 2MB and only two allowed mime types)
$validator = new Validator\Simple('2M', ['image/png', 'image/jpg']);
  • MimeTypeValidator
$mimeTypeValidator = new Validator\MimeTypeValidator(['image/png', 'image/jpg']);
  • SizeValidator
// The 1st parameter is the maximum size while the 2nd is the minimum size
$sizeValidator = new Validator\SizeValidator('3M', '1M');
  • DimensionValidator
$config = [
     'width' => 400,
     'height' => 500
]; 
// Can also contain 'min_width', 'max_width', 'min_height' and 'max_height'

$dimensionValidator = new Validator\DimensionValidator($config);

请记住,通过$fileuploadInstance->addValidator($validator);注册新的验证器(们)

如果您想使用常见的可读文件大小格式,如'1M'、'1G',只需将字符串作为第一个参数传递即可。

$validator = new FileUpload\Validator\Simple('10M', ['image/png', 'image/jpg']);

以下是可能的值列表(B => B; KB => K; MB => M; GB => G)。这些值是二进制惯例,基于1024。

FileNameGenerator

使用FileNameGenerator,您可以更改上传文件保存的文件名。

$fileupload = new \FileUpload($_FILES['files'], $_SERVER);
$filenamegenerator = new FileNameGenerator\Simple();
$fileupload->setFileNameGenerator($filenamegenerator);
  • 自定义
$customGenerator = new FileNameGenerator\Custom($provider);
//$provider can be a string (in which case it is returned as is)
//It can also be a callable or a closure which receives arguments in the other of $source_name, $type, $tmp_name, $index, $content_range, FileUpload $upload
  • MD5
$md5Generator = new FileNameGenerator\MD5($allowOverride);
//$allowOverride should be a boolean. A true value would overwrite the file if it exists while a false value would not allow the file to be uploaded since it already exists.
  • 随机
$randomGenerator = new FileNameGenerator\Random($length);
//Where $length is the maximum length of the generator random name
  • 简单
$simpleGenerator = new FileNameGenerator\Simple();
//Saves a file by it's original name
  • Slug
$slugGenerator = new FileNameGenerator\Slug();
//This generator slugifies the name of the uploaded file(s)

请记住,通过$fileuploadInstance->setFileNameGenerator($generator);注册新的生成器

每次调用setFileNameGenerator都会覆盖当前设置的$generator

回调函数

目前实现的事件

  • completed
$fileupload->addCallback('completed', function(\File $file) {
    // Whoosh!
});
  • beforeValidation
$fileUploader->addCallback('beforeValidation', function (\File $file) {
    // About to validate the upload;
});
  • afterValidation
$fileUploader->addCallback('afterValidation', function (\File $file) {
    // Yay, we got only valid uploads
});

扩展

路径解析器、验证器和文件系统被抽象化的原因,是为了您可以根据自己的需求编写自己的实现(并且,为了单元测试)。库附带了一些“简单”的实现,适合基本需求。例如,您可以编写一个与Amazon S3一起工作的文件系统实现。

许可协议

在MIT许可下授权,请参阅LICENSE文件。