2017 © Pedro Peláez
 

library ohupload

An easy to use file upload class

image

rogerthomas84/ohupload

An easy to use file upload class

  • Friday, December 19, 2014
  • by rogerthomas84
  • Repository
  • 2 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Build Status, (*1)

View on Packagist, (*2)

OhUpload PHP Library

PHP Uploads Made Easy!

Basic Usage

You can run a working example of this file by looking in the example folder., (*3)

<?php
$instance = new Upload('form_file_name');
$instance->setTargetDirectory('/path/to/upload/directory');
try {
    if ($instance->receive() === true) {
        // OK.
        $finalPath = $instance->getFinalPath();
    } else {
        // Not OK!
    }
} catch (\Exception $e) {
    // Something happened, possibly a validator. Take a look in:
    // \OhUpload\Validate\Exception for possible Exceptions.
}

Adding a custom validator

You can easily add a validator to the validation stack. Below is an example of a custom validator. All custom validators must extend \OhUpload\Validate\ValidateBase and override the isValid() method. The expected return from the isValid() method has to be either: * \Exception - don't throw, just return. * bool(true), (*4)

<?php
namespace My\Custom\Validators;

use OhUpload\Validate\ValidateBase
use My\Custom\Validators\Exception\ExampleException;

class MyBasicCheck extends ValidateBase
{
    /**
     * Run a custom validator.
     * @return
     *   \My\Custom\Validators\Exception\ExampleException
     *   boolean true
     */
    public function isValid()
    {
        if (!empty($this->file)) { // The file from the $_FILES array
            return true;
        }

        return new ExampleException('Validation failed');
    }
}

To add it into the process, simply use:, (*5)

<?php
$instance = new Upload('form_file_name');
$instance->setTargetDirectory('/path/to/upload/directory');
$instance->addValidator('/My/Custom/Validators/MyBasicCheck'); // This is a string, not an instance.
try {
    if ($instance->receive() === true) {
        // OK.
        $finalPath = $instance->getFinalPath();
    } else {
        // Not OK!
    }
} catch (\My\Custom\Validators\MyBasicCheck $e) {
    // Wow! You're custom validator failed!
}

The Versions

19/12 2014

dev-master

9999999-dev

An easy to use file upload class

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

ohupload