Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
File uploads with validation and storage strategies
PHP
Tag: 1.3.0

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
src/Upload
tests
LICENSE
README.md
composer.json
phpunit.xml

README.md

Upload

Usage

This component simplifies file validation and uploading. Assume a file is uploaded with this HTML form:

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

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

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

// Validate file upload
$file->addValidations(array(
    // Ensure file is of type "image/png"
    new \Upload\Validation\Mimetype('image/png'),

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

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

How to Install

Install composer in your project:

curl -s https://getcomposer.org/installer | php

Create a composer.json file in your project root:

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

Install via composer:

php composer.phar install

Author

Josh Lockhart

License

MIT Public License

Something went wrong with that request. Please try again.