Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
File uploads with validation and storage strategies
branch: master

Merge pull request #53 from reinink/patch-1

Correct spelling mistake with the word "warning".
latest commit 6dd8917c15
Josh Lockhart authored


This component simplifies file validation and uploading.


Assume a file is uploaded with this HTML form:

<form method="POST" enctype="multipart/form-data">
    <input type="file" name="foo" value=""/>
    <input type="submit" value="Upload File"/>

When the HTML form is submitted, the server-side PHP code can validate and upload the file like this:

$storage = new \Upload\Storage\FileSystem('/path/to/directory');
$file = new \Upload\File('foo', $storage);

// Optionally you can rename the file on upload
$new_filename = uniqid();

// Validate file upload
// MimeType List =>
    // Ensure file is of type "image/png"
    new \Upload\Validation\Mimetype('image/png'),

    //You can also add multi mimetype validation
    //new \Upload\Validation\Mimetype(array('image/png', 'image/gif'))

    // Ensure file is no larger than 5M (use "B", "K", M", or "G")
    new \Upload\Validation\Size('5M')

// Access data about the file that has been uploaded
$data = array(
    'name'       => $file->getNameWithExtension(),
    'extension'  => $file->getExtension(),
    'mime'       => $file->getMimetype(),
    'size'       => $file->getSize(),
    'md5'        => $file->getMd5(),
    'dimensions' => $file->getDimensions()

// Try to upload file
try {
    // Success!
} catch (\Exception $e) {
    // Fail!
    $errors = $file->getErrors();

How to Install

Install composer in your project:

curl -s | php

Create a composer.json file in your project root:

    "require": {
        "codeguy/upload": "*"

Install via composer:

php composer.phar install


Josh Lockhart


MIT Public License

Something went wrong with that request. Please try again.