gargron/fileupload

能够处理大文件、分块上传和多个文件上传的文件上传库

v1.5.1 2019-02-14 10:12 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 FileUpload\Validator\Simple('2M', ['image/png', 'image/jpg']);

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

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

// FileUploader itself
$fileupload = new FileUpload\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 FileUpload\Validator\MimeTypeValidator(['image/png', 'image/jpg']),
        new FileUpload\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 FileUpload\Validator\Simple('2M', ['image/png', 'image/jpg']);
  • MimeTypeValidator
$mimeTypeValidator = new FileUpload\Validator\MimeTypeValidator(['image/png', 'image/jpg']);
  • SizeValidator
// The 1st parameter is the maximum size while the 2nd is the minimum size
$sizeValidator = new FileUpload\Validator\SizeValidator('3M', '1M');
  • DimensionValidator
$config = [
     'width' => 400,
     'height' => 500
]; 
// Can also contain 'min_width', 'max_width', 'min_height' and 'max_height'

$dimensionValidator = new FileUpload\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\FileUpload($_FILES['files'], $_SERVER);
$filenamegenerator = new FileUpload\FileNameGenerator\Simple();
$fileupload->setFileNameGenerator($filenamegenerator);
  • 自定义
$customGenerator = new FileUpload\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 FileUpload\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 FileUpload\FileNameGenerator\Random($length);
//Where $length is the maximum length of the generator random name
  • 简单
$simpleGenerator = new FileUpload\FileNameGenerator\Simple();
//Saves a file by it's original name
  • Slug
$slugGenerator = new FileUpload\FileNameGenerator\Slug();
//This generator slugifies the name of the uploaded file(s)

请通过 $fileuploadInstance->setFileNameGenerator($generator); 注册新验证器(们)

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

回调

目前实现的事件

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

扩展

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

许可证

根据 MIT 许可证授权,请参阅 LICENSE 文件。