PHPackages                             delight-im/file-upload - 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. delight-im/file-upload

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

delight-im/file-upload
======================

Simple and convenient file uploads — secure by default

v1.2.0(8y ago)7310.9k↓74.5%13[2 issues](https://github.com/delight-im/PHP-FileUpload/issues)[1 PRs](https://github.com/delight-im/PHP-FileUpload/pulls)2MITPHPPHP &gt;=5.6.0

Since Mar 11Pushed 3y ago3 watchersCompare

[ Source](https://github.com/delight-im/PHP-FileUpload)[ Packagist](https://packagist.org/packages/delight-im/file-upload)[ Docs](https://github.com/delight-im/PHP-FileUpload)[ RSS](/packages/delight-im-file-upload/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependenciesVersions (5)Used By (2)

PHP-FileUpload
==============

[](#php-fileupload)

Simple and convenient file uploads — secure by default

Requirements
------------

[](#requirements)

- PHP 5.6.0+

Installation
------------

[](#installation)

1. Include the library via Composer [\[?\]](https://github.com/delight-im/Knowledge/blob/master/Composer%20(PHP).md):

    ```
    $ composer require delight-im/file-upload

    ```
2. Include the Composer autoloader:

    ```
    require __DIR__ . '/vendor/autoload.php';
    ```
3. Set up your HTML form for the file upload, e.g.:

    ```

        Upload

    ```

    The two attributes `method="post"` and `enctype="multipart/form-data"` on the `` element are mandatory. Likewise, there must be at least one `` element with a proper `name` attribute. Finally, some way to submit the form, e.g. the `` element, is required. The hidden input named `MAX_FILE_SIZE` is an optional hint for the client.

Usage
-----

[](#usage)

- [File uploads](#file-uploads)
    - [Limiting the maximum permitted file size](#limiting-the-maximum-permitted-file-size)
    - [Reading the maximum permitted file size](#reading-the-maximum-permitted-file-size)
    - [Restricting the allowed file types or extensions](#restricting-the-allowed-file-types-or-extensions)
    - [Reading the allowed file types or extensions](#reading-the-allowed-file-types-or-extensions)
    - [Reading the target directory](#reading-the-target-directory)
    - [Defining the target filename](#defining-the-target-filename)
    - [Reading the target filename](#reading-the-target-filename)
    - [Reading the name of the input field](#reading-the-name-of-the-input-field)
- [Base64 uploads](#base64-uploads)
    - [Limiting the maximum permitted file size](#limiting-the-maximum-permitted-file-size-1)
    - [Reading the maximum permitted file size](#reading-the-maximum-permitted-file-size-1)
    - [Defining the filename extension](#defining-the-filename-extension)
    - [Reading the filename extension](#reading-the-filename-extension)
    - [Reading the target directory](#reading-the-target-directory-1)
    - [Defining the target filename](#defining-the-target-filename-1)
    - [Reading the target filename](#reading-the-target-filename-1)
    - [Reading the Base64 data](#reading-the-base64-data)
- [Data URI uploads](#data-uri-uploads)
    - [Limiting the maximum permitted file size](#limiting-the-maximum-permitted-file-size-2)
    - [Reading the maximum permitted file size](#reading-the-maximum-permitted-file-size-2)
    - [Restricting the allowed MIME types and extensions](#restricting-the-allowed-mime-types-and-extensions)
    - [Reading the allowed MIME types and extensions](#reading-the-allowed-mime-types-and-extensions)
    - [Reading the target directory](#reading-the-target-directory-2)
    - [Defining the target filename](#defining-the-target-filename-2)
    - [Reading the target filename](#reading-the-target-filename-2)
    - [Reading the data URI](#reading-the-data-uri)

### File uploads

[](#file-uploads)

```
$upload = new \Delight\FileUpload\FileUpload();
$upload->withTargetDirectory('/my-app/users/' . $userId . '/avatars');
$upload->from('my-input-name');

try {
    $uploadedFile = $upload->save();

    // success

    // $uploadedFile->getFilenameWithExtension()
    // $uploadedFile->getFilename()
    // $uploadedFile->getExtension()
    // $uploadedFile->getDirectory()
    // $uploadedFile->getPath()
    // $uploadedFile->getCanonicalPath()
}
catch (\Delight\FileUpload\Throwable\InputNotFoundException $e) {
    // input not found
}
catch (\Delight\FileUpload\Throwable\InvalidFilenameException $e) {
    // invalid filename
}
catch (\Delight\FileUpload\Throwable\InvalidExtensionException $e) {
    // invalid extension
}
catch (\Delight\FileUpload\Throwable\FileTooLargeException $e) {
    // file too large
}
catch (\Delight\FileUpload\Throwable\UploadCancelledException $e) {
    // upload cancelled
}
```

#### Limiting the maximum permitted file size

[](#limiting-the-maximum-permitted-file-size)

```
$upload->withMaximumSizeInBytes(4194304);

// or

$upload->withMaximumSizeInKilobytes(4096);

// or

$upload->withMaximumSizeInMegabytes(4);
```

#### Reading the maximum permitted file size

[](#reading-the-maximum-permitted-file-size)

```
// e.g. int(4194304)
$upload->getMaximumSizeInBytes();

// or

// e.g. int(4096)
$upload->getMaximumSizeInKilobytes();

// or

// e.g. int(4)
$upload->getMaximumSizeInMegabytes();
```

#### Restricting the allowed file types or extensions

[](#restricting-the-allowed-file-types-or-extensions)

```
$upload->withAllowedExtensions([ 'jpeg', 'jpg', 'png', 'gif' ]);
```

**Note:** By default, a set of filename extensions is used that is relatively safe for PHP applications and common on the web. This may be sufficient for some use cases.

#### Reading the allowed file types or extensions

[](#reading-the-allowed-file-types-or-extensions)

```
// e.g. array(4) { [0]=> string(4) "jpeg" [1]=> string(3) "jpg" [2]=> string(3) "png" [3]=> string(3) "gif" }
$upload->getAllowedExtensionsAsArray();

// or

// e.g. string(16) "jpeg,jpg,png,gif"
$upload->getAllowedExtensionsAsMachineString();

// or

// e.g. string(19) "JPEG, JPG, PNG, GIF"
$upload->getAllowedExtensionsAsHumanString();

// or

// e.g. string(21) "JPEG, JPG, PNG or GIF"
$upload->getAllowedExtensionsAsHumanString(' or ');
```

#### Reading the target directory

[](#reading-the-target-directory)

```
// e.g. string(24) "/my-app/users/42/avatars"
$upload->getTargetDirectory();
```

#### Defining the target filename

[](#defining-the-target-filename)

```
$upload->withTargetFilename('my-picture');
```

**Note:** By default, a random filename will be used, which is sufficient (and desired) in many cases.

#### Reading the target filename

[](#reading-the-target-filename)

```
// e.g. string(10) "my-picture"
$upload->getTargetFilename();
```

#### Reading the name of the input field

[](#reading-the-name-of-the-input-field)

```
// e.g. string(13) "my-input-name"
$upload->getSourceInputName();
```

### Base64 uploads

[](#base64-uploads)

```
$upload = new \Delight\FileUpload\Base64Upload();
$upload->withTargetDirectory('/my-app/users/' . $userId . '/avatars');
$upload->withData($_POST['my-base64']);

try {
    $uploadedFile = $upload->save();

    // success

    // $uploadedFile->getFilenameWithExtension()
    // $uploadedFile->getFilename()
    // $uploadedFile->getExtension()
    // $uploadedFile->getDirectory()
    // $uploadedFile->getPath()
    // $uploadedFile->getCanonicalPath()
}
catch (\Delight\FileUpload\Throwable\InputNotFoundException $e) {
    // input not found
}
catch (\Delight\FileUpload\Throwable\InvalidFilenameException $e) {
    // invalid filename
}
catch (\Delight\FileUpload\Throwable\InvalidExtensionException $e) {
    // invalid extension
}
catch (\Delight\FileUpload\Throwable\FileTooLargeException $e) {
    // file too large
}
catch (\Delight\FileUpload\Throwable\UploadCancelledException $e) {
    // upload cancelled
}
```

#### Limiting the maximum permitted file size

[](#limiting-the-maximum-permitted-file-size-1)

```
$upload->withMaximumSizeInBytes(4194304);

// or

$upload->withMaximumSizeInKilobytes(4096);

// or

$upload->withMaximumSizeInMegabytes(4);
```

#### Reading the maximum permitted file size

[](#reading-the-maximum-permitted-file-size-1)

```
// e.g. int(4194304)
$upload->getMaximumSizeInBytes();

// or

// e.g. int(4096)
$upload->getMaximumSizeInKilobytes();

// or

// e.g. int(4)
$upload->getMaximumSizeInMegabytes();
```

#### Defining the filename extension

[](#defining-the-filename-extension)

```
$upload->withFilenameExtension('png');
```

**Note:** This defines the filename extension of the file *to be uploaded*, which is a property of the `FileUpload` instance. It does *not* change the extension of any file *already* uploaded, which would be represented in a `File` instance. By default, the filename extension `bin` for arbitrary (binary) data will be used, which may be sufficient in some cases.

#### Reading the filename extension

[](#reading-the-filename-extension)

```
// e.g. string(3) "png"
$upload->getFilenameExtension();
```

**Note:** This retrieves the filename extension of the file *to be uploaded*, which is a property of the `FileUpload` instance. It does *not* read the extension of any file *already* uploaded, which would be represented in a `File` instance.

#### Reading the target directory

[](#reading-the-target-directory-1)

```
// e.g. string(24) "/my-app/users/42/avatars"
$upload->getTargetDirectory();
```

#### Defining the target filename

[](#defining-the-target-filename-1)

```
$upload->withTargetFilename('my-picture');
```

**Note:** By default, a random filename will be used, which is sufficient (and desired) in many cases.

#### Reading the target filename

[](#reading-the-target-filename-1)

```
// e.g. string(10) "my-picture"
$upload->getTargetFilename();
```

#### Reading the Base64 data

[](#reading-the-base64-data)

```
// e.g. string(20) "SGVsbG8sIFdvcmxkIQ=="
$upload->getData();
```

### Data URI uploads

[](#data-uri-uploads)

```
$upload = new \Delight\FileUpload\DataUriUpload();
$upload->withTargetDirectory('/my-app/users/' . $userId . '/avatars');
$upload->withUri($_POST['my-data-uri']);

try {
    $uploadedFile = $upload->save();

    // success

    // $uploadedFile->getFilenameWithExtension()
    // $uploadedFile->getFilename()
    // $uploadedFile->getExtension()
    // $uploadedFile->getDirectory()
    // $uploadedFile->getPath()
    // $uploadedFile->getCanonicalPath()
}
catch (\Delight\FileUpload\Throwable\InputNotFoundException $e) {
    // input not found
}
catch (\Delight\FileUpload\Throwable\InvalidFilenameException $e) {
    // invalid filename
}
catch (\Delight\FileUpload\Throwable\InvalidExtensionException $e) {
    // invalid extension
}
catch (\Delight\FileUpload\Throwable\FileTooLargeException $e) {
    // file too large
}
catch (\Delight\FileUpload\Throwable\UploadCancelledException $e) {
    // upload cancelled
}
```

#### Limiting the maximum permitted file size

[](#limiting-the-maximum-permitted-file-size-2)

```
$upload->withMaximumSizeInBytes(4194304);

// or

$upload->withMaximumSizeInKilobytes(4096);

// or

$upload->withMaximumSizeInMegabytes(4);
```

#### Reading the maximum permitted file size

[](#reading-the-maximum-permitted-file-size-2)

```
// e.g. int(4194304)
$upload->getMaximumSizeInBytes();

// or

// e.g. int(4096)
$upload->getMaximumSizeInKilobytes();

// or

// e.g. int(4)
$upload->getMaximumSizeInMegabytes();
```

#### Restricting the allowed MIME types and extensions

[](#restricting-the-allowed-mime-types-and-extensions)

```
$upload->withAllowedMimeTypesAndExtensions([
    'image/jpeg' => 'jpg',
    'image/png' => 'png',
    'image/gif' => 'gif'
]);
```

**Note:** By default, a set of MIME types is used that is relatively safe for PHP applications and common on the web. This may be sufficient for some use cases.

#### Reading the allowed MIME types and extensions

[](#reading-the-allowed-mime-types-and-extensions)

```
// e.g. array(3) { [0]=> string(10) "image/jpeg" [1]=> string(9) "image/png" [2]=> string(9) "image/gif" }
$upload->getAllowedMimeTypesAsArray();

// or

// e.g. string(30) "image/jpeg,image/png,image/gif"
$upload->getAllowedMimeTypesAsMachineString();

// or

// e.g. string(32) "image/jpeg, image/png, image/gif"
$upload->getAllowedMimeTypesAsHumanString();

// or

// e.g. string(34) "image/jpeg, image/png or image/gif"
$upload->getAllowedMimeTypesAsHumanString(' or ');

// or

// e.g. array(3) { [0]=> string(3) "jpg" [1]=> string(3) "png" [2]=> string(3) "gif" }
$upload->getAllowedExtensionsAsArray();

// or

// e.g. string(11) "jpg,png,gif"
$upload->getAllowedExtensionsAsMachineString();

// or

// e.g. string(13) "JPG, PNG, GIF"
$upload->getAllowedExtensionsAsHumanString();

// or

// e.g. string(15) "JPG, PNG or GIF"
$upload->getAllowedExtensionsAsHumanString(' or ');
```

#### Reading the target directory

[](#reading-the-target-directory-2)

```
// e.g. string(24) "/my-app/users/42/avatars"
$upload->getTargetDirectory();
```

#### Defining the target filename

[](#defining-the-target-filename-2)

```
$upload->withTargetFilename('my-picture');
```

**Note:** By default, a random filename will be used, which is sufficient (and desired) in many cases.

#### Reading the target filename

[](#reading-the-target-filename-2)

```
// e.g. string(10) "my-picture"
$upload->getTargetFilename();
```

#### Reading the data URI

[](#reading-the-data-uri)

```
// e.g. string(43) "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ=="
$upload->getUri();
```

Contributing
------------

[](#contributing)

All contributions are welcome! If you wish to contribute, please create an issue first so that your feature, problem or question can be discussed.

License
-------

[](#license)

This project is licensed under the terms of the [MIT License](https://opensource.org/licenses/MIT).

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity39

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity61

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

Total

4

Last Release

3034d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6597567?v=4)[delight.im](/maintainers/delight-im)[@delight-im](https://github.com/delight-im)

---

Top Contributors

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

---

Tags

filefilesfilesystemforminputphpuploadvalidationvalidationfilefilesuploadforminput

### Embed Badge

![Health badge](/badges/delight-im-file-upload/health.svg)

```
[![Health](https://phpackages.com/badges/delight-im-file-upload/health.svg)](https://phpackages.com/packages/delight-im-file-upload)
```

###  Alternatives

[kartik-v/bootstrap-fileinput

An enhanced HTML 5 file input for Bootstrap 5.x, 4.x, and 3.x with features for file preview for many file types, multiple selection, ajax uploads, and more.

5.4k8.2M13](/packages/kartik-v-bootstrap-fileinput)[kartik-v/yii2-widget-fileinput

An enhanced FileInput widget for Bootstrap 3.x, 4.x &amp; 5.x with file preview, multiple selection, and more features (sub repo split from yii2-widgets)

2357.1M97](/packages/kartik-v-yii2-widget-fileinput)

PHPackages © 2026

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