#6 laravel-fileupload

File uploading library capable of handling large/chunked/multiple file uploads

FileUpload

PHP FileUpload library that supports chunked uploads. Adopted from the procedural script included with jQuery-File-Upload, designed to work with that JavaScript plugin, with normal forms, and to be embeddable into any application/architecture.

Installing

This package is available via Composer:

{
  "require": {
    "gargron/fileupload": "~1.0.*"
  }
}

Status

The unit test suite covers simple uploads, and the library “works on my machine,” as it were. You are welcome to contribute.

You can grep the source code for “TODO” to find things you could help finishing.

Usage

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

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

// The machine's filesystem
$filesystem = new FileUploadFileSystemSimple();

// FileUploader itself
$fileupload = new FileUploadFileUpload($_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(array('files' => $files));

Validator

If you want you can use the common human readable format for filesizes like “1M”, “1G”, just pass the String as the first Argument.

$validator = new FileUploadValidatorSimple("10M", ['image/png', 'image/jpg']);

Here is a listing of the possible values (B => B; KB => K; MB => M; GB => G). These values are Binary convention so basing on 1024.

FileNameGenerator

With the FileNameGenerator you have the possibility to change under witch Filename we uploaded files will be saved.

$fileupload = new FileUploadFileUpload($_FILES['files'], $_SERVER);
$filenamegenerator = new FileUploadFileNameGeneratorSimple();
$fileupload->setFileNameGenerator($filenamegenerator);

We have placed some example generators like md5 who saves the file under the md5 hash of the filename or the random generator witch uses an random string. The default (the simple generator to be more precise) will save the file by its origin name.

Callbacks

Currently implemented events:

  • completed
$fileupload->addCallback('completed', function(FileUploadFile $file) {
  // Whoosh!
});

Extending

The reason why the path resolver, the validators and the file system are abstracted, is so you can write your own, fitting your own needs (and also, for unit testing). The library is shipped with a bunch of “simple” implementations which fit the basic needs. You could write a file system implementation that works with Amazon S3, for example.