PHPackages                             craigh/file-uploader - 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. craigh/file-uploader

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

craigh/file-uploader
====================

Allows file uploads via web form

1.0.3(9y ago)44011mitPHP

Since Sep 16Pushed 9y ago1 watchersCompare

[ Source](https://github.com/craigh411/FileUploader)[ Packagist](https://packagist.org/packages/craigh/file-uploader)[ RSS](/packages/craigh-file-uploader/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (2)Versions (5)Used By (1)

FileUploader
============

[](#fileuploader)

[![Build Status](https://camo.githubusercontent.com/8de6e80ca4251f63e00df9e894610a8f0423664e67a2e319e44e1b897b5def82/68747470733a2f2f7472617669732d63692e6f72672f6372616967683431312f46696c6555706c6f616465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/craigh411/FileUploader)

An easily configurable file uploader for uploading files via a web form

Features
--------

[](#features)

- Set accepted file types
- Block unwanted file types
- Set maximum file size
- Option to auto create upload directories if they do not exist
- Option to overwrite files
- Option to automatically create unique filenames
- Filename sanitisation

Installing
----------

[](#installing)

The easiest way to install is via Composer:

`composer require craigh/file-uploader`

or you can add `craigh/file-uploader` to your composer.json file and run `composer update`

**Note:** The FileUploader requires [`symfony/HttpFoundation`](https://github.com/symfony/HttpFoundation), so if you are not using composer you will need to make sure you have this library in your include path.

Usage
-----

[](#usage)

The FileUploader requires a `Symfony\Component\HttpFoundation\File\UploadedFile` object. You can easily retrieve an instance by passing the $\_FILES\['Your\_field\_name'\] variable into the getUploadedFile() method on the `Humps\FileUploader\File` class:

`$file = Humps\FileUploader\File::getUploadedFile($_FILES['file']);`

This can then be passed in to the FileUploader:

`$uploader = new Humps\FileUploader\FileUploader($file);`

You can then upload the file as follows:

`$uploader->upload();`

Options
-------

[](#options)

##### uploadDir(string)

[](#uploaddirstring)

Sets the upload path. It will also append any required '/' if it is not set, so both 'path/to/dir' and 'path/to/dir/' will work (defaults to current directory)

`$uploader->uploadDir('path/to/dir');`

##### overwrite(boolean)

[](#overwriteboolean)

Set to true to allow overwriting of files with the same name (default: false)

`$uploader->overwrite(true);`

##### allowedMimeTypes(array)

[](#allowedmimetypesarray)

Pass in an array of allowed mime types, everything else will be blocked. When empty all file types will be allowed unless explicitly blocked.

`$uploader->allowedMimeTypes(['image/jpeg,'image/png', 'image/gif']);`

##### blockedMimeTypes(array)

[](#blockedmimetypesarray)

You can also block file types if you prefer. Pass in an array of mime types you want to block

`$uploader->blockedMimeTypes(['application/x-msdownload']);`

##### maxFileSize($size, $unit)

[](#maxfilesizesize-unit)

The maximum file size you want to allow, expects size to be a number and unit to be either:

- B - Byte
- KB - Kilobyte
- MB - Megabyte

`$uploader->maxFileSize(5, 'MB');`

You can also use the words BYTE, BYTES, KILOBYTE, KILOBYTES, MEGABYTE or MEGABYTES if you prefer:

`$uploader->maxFileSize(1, 'MEGABYTE');`

##### createDirs(bool)

[](#createdirsbool)

If set to true this will recursively create any specified directories if they do not exist (default: false)

`$uploader->createDirs(true);`

##### makeFilenameUnique(bool)

[](#makefilenameuniquebool)

If set to true this will make the filename unique by appending a \_{number} to the end.

`$uploader->makeFilenameUnique(true);`

##### filename(string)

[](#filenamestring)

By default the filename will be a sanitised version of the uploaded filename. Use this method if you want to set your own filename.

`$uploader->filename('myFile.txt');`

**Note:** When using this method the filename will not be sanatised, if you want to sanatise the filename you can use the sanitizeFilename() method.

##### sanitizeFilename()

[](#sanitizefilename)

Sanitises the given filename by removing any non alpha numeric characters and replacing any spaces with an underscore. You will only need to call this if you want to set your own filenames using the filename() method, otherwise this method is called automatically. You should also be aware that this call will need to be made after you set your filename:

```
$uploader->filename('my%$crazy@filename.txt')->sanitizeFilename();

```

##### upload()

[](#upload)

Uploads the file and returns the upload path.

`$uploadPath = $uploader->upload();`

upload() is an alias of move(), so you can also use the move() method if you feel it's wording is more appropriate:

`$uploadPath = $uploader->move();`

Chaining
--------

[](#chaining)

All methods above can be applied in a chain for a clean syntax:

```
use Humps\FileUploader\File;
use Humps\FileUploader\FileUploader;

$file = File::getUploadedFile($_FILE['file']);
$uploader = new FileUploader($file);
$uploader->uploadPath->('files')->overwrite(true)->upload();

```

or even

```
use Humps\FileUploader\File;
use Humps\FileUploader\FileUploader;

$file = File::getUploadedFile($_FILE['file']);
$uploader = (new FileUploader($file))->upload();

```

Config by Extending the FileUploader Class
------------------------------------------

[](#config-by-extending-the-fileuploader-class)

For a cleaner way to configure you uploads you can extend the FileUploader class which will give you access to the protected variables, e.g.:

```
use Humps\FileUploader\FileUploader;

class ImageUploader extends FileUploader{

	protected $allowedMimeTypes = [
		'image/jpeg',
		'image/png',
		'image/gif'
	];

	protected $maxFileSize = 5e+6; // In bytes (this is 5MB or 5000000 bytes)
	protected $makeFilenameUnique = true;
	protected $createDirIfNotExists = true;
}

```

This can then be used as follows:

```
use Humps\FileUploader\File;

$image = File::getUploadedFile($_FILE['image']);
$uploader = new ImageUploader($image);
$image->upload();

```

The following variables are protected and so can be set by child classes:

```
protected $uploadDir; // Upload directory
protected $allowedMimeTypes = []; // Only allow these file to be uploaded
protected $blockedMimeTypes = []; // Don't allow these files to be uploaded
protected $maxFileSize = 1000000; // In bytes
protected $makeFilenameUnique = false; // Make the filename unique if two files have the same name
protected $overwrite = false; // Allow overwriting of files with the same name
protected $createDirs = false; // Allow the automatic creation of any upload directories

```

A note on the examples
----------------------

[](#a-note-on-the-examples)

If you want to run the examples then you will need to change the include path to point to your autoloader or include the FileUploader classes manually.

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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 ~186 days

Total

4

Last Release

3337d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/791b31f6b625d43874f70831846b002b7bb46dc911e38b02250e7aaab6596716?d=identicon)[craigh411](/maintainers/craigh411)

---

Top Contributors

[![craigh411](https://avatars.githubusercontent.com/u/13747552?v=4)](https://github.com/craigh411 "craigh411 (39 commits)")

---

Tags

filesuploaduploader

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/craigh-file-uploader/health.svg)

```
[![Health](https://phpackages.com/badges/craigh-file-uploader/health.svg)](https://phpackages.com/packages/craigh-file-uploader)
```

###  Alternatives

[vich/uploader-bundle

Ease file uploads attached to entities

1.9k25.9M116](/packages/vich-uploader-bundle)[uploadcare/uploadcare-php

Uploadcare PHP integration handles uploads and further operations with files by wrapping Upload and REST APIs.

1022.5M6](/packages/uploadcare-uploadcare-php)[unclecheese/dropzone

An HTML5 upload field for the CMS and frontend forms.

46130.7k6](/packages/unclecheese-dropzone)[fof/upload

The file upload extension for the Flarum forum with insane intelligence.

188171.7k15](/packages/fof-upload)[vova07/yii2-fileapi-widget

The FileAPI widget for Yii2 framework.

4765.5k9](/packages/vova07-yii2-fileapi-widget)[delight-im/file-upload

Simple and convenient file uploads — secure by default

7210.7k2](/packages/delight-im-file-upload)

PHPackages © 2026

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