Code Monkey home page Code Monkey logo

fileupload's Introduction

FileUpload

Build Status

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.1.*"
  }
}

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 FileUpload\Validator\Simple(1024 * 1024 * 2, ['image/png', 'image/jpg']);

// Alternatively, if you don't want to validate both size and type at the same time, you could use:
$mimeTypeValidator = new \FileUpload\Validator\MimeTypeValidator(["image/png", "image/jpeg"]);

// And/or (the 1st parameter is the max size while the 2nd is the min size):
$sizeValidator = new \FileUpload\Validator\SizeValidator("3M", "1M");

// 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(array('files' => $files));

Alternative usage via factory

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

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

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 FileUpload\Validator\Simple("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 the Filename the uploaded files will be saved as.

$fileupload = new FileUpload\FileUpload($_FILES['files'], $_SERVER);
$filenamegenerator = new FileUpload\FileNameGenerator\Simple();
$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(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
});

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.

License

Licensed under the MIT license, see LICENSE file.

fileupload's People

Contributors

gargron avatar gitall avatar jaketoolson avatar milosh012 avatar snipe avatar thenovacreator avatar thers avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.