PHPackages                             gargron/fileupload - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. gargron/fileupload

ActiveLibrary[File &amp; Storage](/categories/file-storage)

gargron/fileupload
==================

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

v1.5.1(7y ago)457566.2k—8.6%86[9 issues](https://github.com/Gargron/fileupload/issues)[1 PRs](https://github.com/Gargron/fileupload/pulls)7MITPHPPHP ~7.0

Since Aug 25Pushed 4y ago31 watchersCompare

[ Source](https://github.com/Gargron/fileupload)[ Packagist](https://packagist.org/packages/gargron/fileupload)[ RSS](/packages/gargron-fileupload/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (22)Used By (7)

FileUpload
==========

[](#fileupload)

[![Build Status](https://camo.githubusercontent.com/dd140afd0b0fe6e3daf5c9edf44d1b6930ba148b58f4d680e99f948447a7ae29/68747470733a2f2f7472617669732d63692e6f72672f47617267726f6e2f66696c6575706c6f61642e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/Gargron/fileupload)

PHP FileUpload library that supports chunked uploads. Adopted from the procedural script included with [jQuery-File-Upload](https://github.com/blueimp/jQuery-File-Upload), designed to work with that JavaScript plugin, with normal forms, and to be embeddable into any application/architecture.

### Installing

[](#installing)

This package is available via Composer:

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

### Requirements

[](#requirements)

- Ensure that the PHP extension "php\_fileinfo" is enabled;
- Your php.ini must have the next directive:

`file_uploads = On`

### Status

[](#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

[](#usage)

```
// 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());
    }
}
```

### Alternative usage via factory

[](#alternative-usage-via-factory)

```
$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);
```

### Validators

[](#validators)

There are currently 4 validators shipped with `FileUpload`:

- `Simple`

```
// 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);
```

> Remember to register new validator(s) by `$fileuploadInstance->addValidator($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 =&gt; B; KB =&gt; K; MB =&gt; M; GB =&gt; G). These values are binary convention so basing on 1024.

### FileNameGenerator

[](#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);
```

- `Custom`

```
$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.
```

- `Random`

```
$randomGenerator = new FileUpload\FileNameGenerator\Random($length);
//Where $length is the maximum length of the generator random name
```

- `Simple`

```
$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)
```

> Remember to register new validator(s) by `$fileuploadInstance->setFileNameGenerator($generator);`

> Every call to `setFileNameGenerator` overrides the currently set `$generator`

### Callbacks

[](#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

[](#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

[](#license)

Licensed under the MIT license, see `LICENSE` file.

###  Health Score

48

—

FairBetter than 95% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity58

Moderate usage in the ecosystem

Community37

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~117 days

Recently: every ~174 days

Total

18

Last Release

2651d ago

Major Versions

v0.1 → v1.0.02013-09-19

### Community

Maintainers

![](https://www.gravatar.com/avatar/8ae5a57ae8761c3e7a3c88743e8c666ad76604afb46f0d99a7b83573a6e4d144?d=identicon)[Gargron](/maintainers/Gargron)

---

Top Contributors

[![Gargron](https://avatars.githubusercontent.com/u/184731?v=4)](https://github.com/Gargron "Gargron (39 commits)")[![adelowo](https://avatars.githubusercontent.com/u/12677701?v=4)](https://github.com/adelowo "adelowo (20 commits)")[![radmax](https://avatars.githubusercontent.com/u/1110762?v=4)](https://github.com/radmax "radmax (17 commits)")[![thenovacreator](https://avatars.githubusercontent.com/u/2353240?v=4)](https://github.com/thenovacreator "thenovacreator (16 commits)")[![fansanelli](https://avatars.githubusercontent.com/u/13917866?v=4)](https://github.com/fansanelli "fansanelli (3 commits)")[![Indigo744](https://avatars.githubusercontent.com/u/7137528?v=4)](https://github.com/Indigo744 "Indigo744 (3 commits)")[![thers](https://avatars.githubusercontent.com/u/213272?v=4)](https://github.com/thers "thers (2 commits)")[![milosh012](https://avatars.githubusercontent.com/u/1167990?v=4)](https://github.com/milosh012 "milosh012 (2 commits)")[![gitall](https://avatars.githubusercontent.com/u/9973818?v=4)](https://github.com/gitall "gitall (1 commits)")[![netsnatch](https://avatars.githubusercontent.com/u/11537020?v=4)](https://github.com/netsnatch "netsnatch (1 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")[![kaperys](https://avatars.githubusercontent.com/u/7786586?v=4)](https://github.com/kaperys "kaperys (1 commits)")[![SilverFire](https://avatars.githubusercontent.com/u/4499203?v=4)](https://github.com/SilverFire "SilverFire (1 commits)")[![snipe](https://avatars.githubusercontent.com/u/197404?v=4)](https://github.com/snipe "snipe (1 commits)")[![jaketoolson](https://avatars.githubusercontent.com/u/612332?v=4)](https://github.com/jaketoolson "jaketoolson (1 commits)")[![kruglikdenis](https://avatars.githubusercontent.com/u/9932991?v=4)](https://github.com/kruglikdenis "kruglikdenis (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/gargron-fileupload/health.svg)

```
[![Health](https://phpackages.com/badges/gargron-fileupload/health.svg)](https://phpackages.com/packages/gargron-fileupload)
```

###  Alternatives

[knplabs/gaufrette

PHP library that provides a filesystem abstraction layer

2.5k39.8M123](/packages/knplabs-gaufrette)[superbalist/flysystem-google-storage

Flysystem adapter for Google Cloud Storage

26320.6M30](/packages/superbalist-flysystem-google-storage)[illuminate/filesystem

The Illuminate Filesystem package.

15161.6M2.6k](/packages/illuminate-filesystem)[creocoder/yii2-flysystem

The flysystem extension for the Yii framework

2931.7M62](/packages/creocoder-yii2-flysystem)[flowjs/flow-php-server

PHP library for handling chunk uploads. Works with flow.js html5 file uploads.

2451.6M15](/packages/flowjs-flow-php-server)[madnest/madzipper

Easier zip file handling for Laravel applications.

1382.3M6](/packages/madnest-madzipper)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
