2017 © Pedro Peláez
 

symfony-bundle file-upload-bundle

This bundle provides a simple file upload bundle

image

ns/file-upload-bundle

This bundle provides a simple file upload bundle

  • Wednesday, June 13, 2018
  • by gnat
  • Repository
  • 1 Watchers
  • 1 Stars
  • 1,237 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 9 Versions
  • 10 % Grown

The README.md

File Upload Bundle

A simple bundle to handle file uploads and url generation., (*1)

Install

Via Composer, (*2)

$ composer require ns/file-upload-bundle

Configuration

Define a configuration

Configurations are defined as services. For each naming / file location define a tagged service. You can choose any of the provided file namers, or implement your own. Only one of the second or third parameters are required, however both can be used as well., (*3)

# services.yml
ns_file.applicant_uploads:
  class: NS\FileUploadBundle\Handler\Config
  arguments: [ "@ns_file.namer.default", "applicant-uploads", "@ns_file.directory_namer.hash" ]
  tags:
    - { name: ns_file.config, config_name: applicant }

Given the above configuration. A file uploaded using this configuration would be stored in web/uploads/applicant-uploads/HASH/ClientOriginalName_RANDSTR.ext, (*4)

You are free to implement your own file and/or directory namers. Simply implement the NS\FileUploadBundle\Namer\FileNamerInterface or NS\FileUploadBundle\Namer\DirectoryNamerInterface., (*5)

The bundle comes with the following file naming strategies., (*6)

OriginalNamer

This stores the uploaded file using the name provided by the client when uploaded. Service id @ns_file.namer.client_original, (*7)

OriginalRandomNamer

Similar to the OriginalNamer, this will keep the original filename, but inserts random characters and numbers after the name but before the file extension. This is the default file namer. Service id @ns_file.namer.original_random, (*8)

RandomNamer

This renames the file to a random string but keeps the original file extension. Service id @ns_file.namer.random, (*9)

UniqueHashNamer

This performs a sha1 hash of the original client name and uses that plus the original file extension. Service id @ns_file.namer.unique_hash, (*10)

Usage

When handling a file upload. Simply request the upload handler service. Pass it the instance of the UploadedFile, the configuration and optionally any additional data that the directory namer will use., (*11)

Upload Handling

    // $applicantEmailAddress = 'user@example.com';
    // instanceof UploadedFile with clientOriginalName 'funny-cat-image.jpg'
    $sourceFile = $form['somefile']->getData();
    $handler = $this->get('ns_file.upload_handler');

    $destinationFile = $handler->upload('applicant', $sourceFile, $applicantEmailAddress);

Based on our example configuration above will take the source file and place it in web/uploads/applicant-uploads/63a710569261a24b3766275b7000ce8d7b32e2f7/funny-cat-image_xx12xa.jpg. It will then return a File instance for this. You can save the filename however you'd like., (*12)

Downloads / Twig

There is a twig helper which will allow the generation of download urls for any given file., (*13)

    <a href="{{ file_path('applicant', 'funny-cat-image_xx12xa.jpg', 'user@example.com') }}">Download</a>

Provides a link to this file., (*14)

License

MIT, see LICENSE., (*15)

The Versions

13/06 2018

dev-master

9999999-dev

This bundle provides a simple file upload bundle

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathanael d. Noblet

13/06 2018

0.1.7

0.1.7.0

This bundle provides a simple file upload bundle

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathanael d. Noblet

13/06 2018

0.1.6

0.1.6.0

This bundle provides a simple file upload bundle

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathanael d. Noblet

11/01 2017

0.1.5

0.1.5.0

This bundle provides a simple file upload bundle

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathanael d. Noblet

17/10 2016

0.1.4

0.1.4.0

This bundle provides a simple file upload bundle

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathanael d. Noblet

23/08 2016

0.1.3

0.1.3.0

This bundle provides a simple file upload bundle

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathanael d. Noblet

22/08 2016

0.1.2

0.1.2.0

This bundle provides a simple file upload bundle

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathanael d. Noblet

22/08 2016

0.1.1

0.1.1.0

This bundle provides a simple file upload bundle

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathanael d. Noblet

22/08 2016

0.1.0

0.1.0.0

This bundle provides a simple file upload bundle

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathanael d. Noblet