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

  • Monday, May 21, 2018
  • by slince
  • Repository
  • 1 Watchers
  • 3 Stars
  • 168 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 3 % Grown

The README.md

FileUpload

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

Process uploaded files with multiple and extensible validation rules., (*2)

Installation

Install via composer, (*3)

$ composer require slince/upload

Quick view

$builder = new Slince\Upload\UploadHandlerBuilder(); //create a builder.
$handler = $builder
    ->saveTo(__DIR__ . '/dst')
    ->getHandler();

$files = $handler->handle();
print_r($files);

Usage

Assume files are uploaded with this HTML form:, (*4)

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

Server:, (*5)

$builder = new Slince\Upload\UploadHandlerBuilder(); //create a builder.
$handler = $builder
    ->overwrite(true) // open overwrite mode. 

    //Custom namer
    ->naming(function (UploadedFile $file) {
        return date('Y/md') . '/' . uniqid() . '.' . $file->getClientOriginalExtension();
    })

    //add constraints
    ->sizeBetween('10m', '20m')
    ->allowExtensions(['jpg', 'txt'])
    ->allowMimeTypes(['image/*', 'text/plain'])

    ->saveTo(__DIR__ . '/dst') //save to local
    ->getHandler();

$files = $handler->handle();

foreach ($files as $file) {
    $uploadedFile = $file->getUploadedFile();
    if ($file->isUploaded()) {
        echo $uploadedFile->getClientOriginalName() . ' upload ok, path:' . $file->getMetadata('spl_file')->getPathname();
    } else {
        echo $uploadedFile->getClientOriginalName() . ' upload error: ' . $file->getException()->getMessage();
    }
    echo PHP_EOL;
}

File metadata $file->getMetadata('metadata name') is provided by storage layer. if you are using Local, the file has a metadata named 'spl_file', it is an instance of SplFileInfo., (*6)

If you want access attributes of the file saved in the client, you can use like this., (*7)

$files['foo']->getUploadedFile()->getClientOriginalName(); // original name
$files['bar']['baz'][0]->getUploadedFile()->getClientOriginalExtension(); // original  extension
$files['bar']['baz'][1]->getUploadedFile()->getClientMimeType(); // original  mime type

Integration with flysystem

function createS3Flysystem()
{
    $client = new Aws\S3\S3Client([
        'credentials' => [
            'key'    => 'your-key',
            'secret' => 'your-secret'
        ],
        'region' => 'your-region',
        'version' => 'latest|version',
    ]);
    $adapter = new League\Flysystem\AwsS3v3\AwsS3Adapter($client, 'your-bucket-name');
    $flysystem = new League\Flysystem\Filesystem($adapter);
    return $flysystem;
}

$builder = new Slince\Upload\UploadHandlerBuilder(); //create a builder.
$handler = $builder->setFilesystem(new Slince\Upload\Filesystem\Flysystem(createS3Flysystem()))
    ->getHandler();

$files = $handler->handle();
print_r($files);

All files will be automatically uploaded to AWS S3., (*8)

License

The MIT license. See MIT, (*9)

The Versions

21/05 2018

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