2017 © Pedro Peláez
 

library upload

Process file uploads with multiple and extensible validation rules

image

slince/upload

Process file uploads with multiple and extensible validation rules

  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 10 % Grown

The README.md

FileUpload

Build Status Coverage Status Latest Stable Version Scrutinizer, (*1)

Process uploaded files., (*2)

Install

Install via composer, (*3)

composer require slince/upload

Usage

Assume a file is uploaded with this HTML form:, (*4)

<form method="POST" enctype="multipart/form-data">
    <input type="file" name="foo" value=""/>
    <input type="submit" value="Upload File"/>
</form>
  • Basic usage
use Slince\Upload\Uploader;
use Slince\Upload\Exception\UploadException;
use Slince\Upload\FileInfo;

$uploader = new Uploader('./savepath');

try {
    $fileInfo = $uploader->process($_FILES['foo']);

    if ($fileInfo->hasError()) {
        echo $fileInfo->getErrorCode();
        echo $fileInfo->getErrorMsg();
    } else {
        echo $fileInfo->getPath(); //Gets the saved path of the uploaded file
    }
} catch (UploadException $exception) {
     exit($e->getMessage());
}

  • Advanced usage
//Override old files if there is a file of the same name  
$uploader->setOverride(true);

//Generate new file name using random mode
$uploader->setRandName(true);

//Customize the file path
$uploader->setFilenameGenerator(function(FileInfo $file) use ($uploader){
    return $uploader->getSavePath() . uniqid() . ".{$file->getExtension()}";
});

//Limit file size, Include boundary values;(unit: byte.) 
//You can alse use human readable size express,e.g 5M (use "B", "K", M", or "G"))
$uploader->addRule(new SizeRule(1000, 2000));


//Limit the file mime type
$uploader->addRule(new MimeTypeRule(['image/*', 'text/planin']));

//Limit the extension
$uploader->addRule(new ExtensionRule(['jpg', 'text']));
  • Multi-file upload
<form method="POST" enctype="multipart/form-data">
    <input type="file" name="foo[]" value=""/>
    <input type="file" name="foo[]" value=""/>
    <input type="submit" value="Upload File"/>
</form>

$uploader->process($_FILES['foo']) will return an array containing all the fileinfo, (*5)

try {
    $fileInfos = $uploader->process($_FILES['foo']);
    var_dump($fileInfos);
} catch (UploadException $exception) {
     exit($e->getMessage());
}

The Versions

05/05 2017

dev-master

9999999-dev

Process file uploads with multiple and extensible validation rules

  Sources   Download

MIT Apache-2.0

The Requires

  • php >=5.5.9

 

The Development Requires

by tao

file validation upload file upload

05/05 2017

1.0.1

1.0.1.0

Process file uploads with multiple and extensible validation rules

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by tao

file validation upload file upload

14/04 2017

1.0.0

1.0.0.0

Handle file uploads with multiple and extensible validation rules

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by tao

file validation upload file-upload