2017 © Pedro Peláez
 

package laravel-file-uploader

Web Form File Uploader for Laravel

image

craigh/laravel-file-uploader

Web Form File Uploader for Laravel

  • Wednesday, March 29, 2017
  • by craigh411
  • Repository
  • 1 Watchers
  • 3 Stars
  • 103 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 2 % Grown

The README.md

Laravel File Uploader

Laravel File Uploader is a web form upload manager for Laravel. This package provides additional files for the craigh411/FileUploader package for use within the Laravel framework. FileUploader uses the same symfony/HttpFoundation package used by laravel for file uploads, so is easily integrated, but adds some additional functionality to make file uploads a breeze., (*1)

Features

  • Allow and block specific mime types.
  • Adjust maximum file size.
  • Choose to automatically create upload directories.
  • Allow file overwriting
  • Automatically create sanitised file names.
  • Create unique file names

Installation

To install simply require craigh/laravel-file-uploader in your composer.json file and run composer update., (*2)

You should also register the service provider by adding the following to providers in config/app.php:, (*3)

Humps\LaravelFileUploader\FileUploaderServiceProvider::class, (*4)

Creating an Alias (Optional)

While not required, if you want to use the facade you need to add the following to aliases in config/app.php:, (*5)

'Upload' => Humps\LaravelFileUploader\Facades\UploaderFacade::class, (*6)

You will then be able to use the Upload alias to use the facade., (*7)

Basic Usage

Beacuse LaravelFileUploader uses the symfony/HttpFoundation you can pass $request->file('input_name') directly into the file method., (*8)

If you've added the alias for the facade, then you can use:, (*9)

Upload::file($request->file('file'))->move();, (*10)

You will need to use Upload to get this to work if you are working inside a namespaced class., (*11)

If you are not using the facade you can type hint LaravelFileUploader in your classes methods or constructor or if you prefer you can simply call:, (*12)

$upload = App::make('upload');, (*13)

Changing Settings Directly

If you want to change the default settings then you can simply chain them together like so (See Methods at the bottom for the full settings list):, (*14)

Upload::file($request->all())
->createDirs(true)
->maxFileSize(5,'MB')
->allowedMimeTypes(['image/jpeg', 'image/png', 'image/gif'])
->move();

Note: The move() method returns the upload path, so must be chained to the end., (*15)

Configuring Using Inheritance

For a clean configuration you can extend the LaravelFileUploader class which gives you access to the following protected variables:, (*16)

protected $uploadDir; // Upload directory
protected $allowedMimeTypes = []; // Only allow these file to be uploaded
protected $blockedMimeTypes = []; // Don't allow these files to be uploaded
protected $maxFileSize = 1000000; // In bytes
protected $makeFilenameUnique = false; // Make the filename unique if two files have the same name
protected $overwrite = false; // Allow overwriting of files with the same name
protected $createDirs = false; // Allow the automatic creation of any upload directories

e.g. For an ImageUploader you may do the following:, (*17)

use Humps\LaravelFileUploader\LaravelFileUploader;

class ImageUploader extends LaravelFileUploader{

   protected $uploadDir = 'images/';
   protected $maxFileSize = 5+6e; // 5 MB
   protected $createDirs = true;
   protected $makeFilenameUnique = true;

   protected $allowedMimeTypes = [
    'image/jpeg',
    'image/png',
    'image/gif'
   ];

}

This can now be used in your classes which will automatically use those values as defaults:, (*18)

/**
 * Example using dependency injection
 **/
private $upload;

function __construct(ImageUploader $upload){
    $this->upload = $upload;
}

public function store(Request $request){
    $this->upload->file($request->file('file'))->move();
}

Handling Exceptions

LaravelFileUploader is shipped with an exception handler which redirects the user back to the page with any errors. To use the exception handler simply add the following to the app\Exceptions\Handler.php files' render() method:, (*19)

$uploadExceptionHandler = app()->make('uploadExceptionHandler', [$e]);
if ($errors = $uploadExceptionHandler->getUploadErrors()) {
  return $errors;
}

You can retreive the error message in your view from the $errors variable: (See: http://laravel.com/docs/5.1/validation#quick-displaying-the-validation-errors), (*20)

Custom Error Messages

If you would like to create your own custom error messages for when an exception is thrown, you will need to do:, (*21)

php artisan vendor:publish, (*22)

which will publish the exceptions file to app\resources\lang\vendor\laravel-file-uploader\en which contains the error messages which are displayed for each exception., (*23)

Note: There currently aren't any localised error messages, so you will need to create these yourself if you do not want to use English., (*24)

Custom Exception Behaviour

If you would like to use the LaravelFileUploaderExceptionHandler but don't like the default behaviour of redirecting the user back, then you should override the response() method of the exception handler by extending the class. You will of course, need to change the code in your boot() method to reference your child class., (*25)

use Humps\LaravelFileUploader\LaravelFileUploaderExceptionHandler as BaseHandler;

class CustomUploadExceptionHandler extends BaseHandler {
  protected function response(){
    die($this->error);
  }
}

Humps\FileUploader\Uploader Interface

You can also bind to the Humps\FileUploader\Uploader interface. To do this place the following inside the register() method of your AppServiceProvider:, (*26)

App::bind('Humps\FileUploader\Contracts\Uploader','Humps\LaravelFileUploader\LaravelFileUploader');;, (*27)

You can then type hint the interface instead of the concrete class:, (*28)

use Humps\FileUploader\Uploader;

private $upload;

function __construct(Uploader $upload){
    $this->upload = $upload;
}

Now you can swap out your own implementations simply by changing the bound class e.g.:, (*29)

App::bind('Humps\FileUploader\Contracts\Uploader','Your\Namespace\YourFileUploader');;, (*30)

Methods

uploadDir(string)

Sets the upload directory. It will also append any required '/' if it is not set, so both 'path/to/dir' and 'path/to/dir/' will work., (*31)

$upload->uploadDir('path/to/dir');, (*32)

Note: The public folder will be the base directory for your uploads, so if you set your upload path to 'images' ($upload->uploadDir('images');) this will upload to the public/images folder., (*33)

overwrite(boolean)

Set to true to allow overwriting of files with the same name (default: false), (*34)

$upload->overwrite(true);, (*35)

allowedMimeTypes(array)

Pass in an array of allowed mime types, everything else will be blocked. When empty all file types will be allowedunless explicitly blocked., (*36)

$upload->allowedMimeTypes(['image/jpeg, 'image/png', 'image/gif']);, (*37)

blockedMimeTypes(array)

You can also block file types if you prefer. Pass in an array of mime types you want to block, (*38)

$upload->blockedMimeTypes(['application/x-msdownload']);, (*39)

maxFileSize($size, $unit)

The maximum file size you want to allow, expects size to be a number and unit to be either: - B - Byte - KB - Kilobyte - MB - Megabyte, (*40)

$upload->maxFileSize(5, 'MB');, (*41)

You can also use the words BYTE, BYTES, KILOBYTE, KILOBYTES, MEGABYTE or MEGABYTES if you prefer:, (*42)

$upload->maxFileSize(1, 'MEGABYTE');, (*43)

createDirs(bool)

If set to true this will recursively create any specified directories if they do not exist (default: false), (*44)

$upload->createDirs(true);, (*45)

makeFilenameUnique(bool)

If set to true this will make the filename unique by appending a _{number} to the end., (*46)

$upload->makeFilenameUnique(true);, (*47)

filename(string)

By default the filename will be a sanitised version of the uploaded filename. Use this method if you want to set your own filename., (*48)

$upload->filename('myFile.txt');, (*49)

Note: When using this method the filename will not be sanatised, if you want to sanatise the filename you can use the sanitizeFilename() method., (*50)

sanitizeFilename()

Sanitises the given filename by removing any non alpha numeric characters and replacing any spaces with an underscore. You will only need to call this if you want to set your own filenames using the filename() method, otherwise this method is called automatically. You should also be aware that this call will need to be made after you set your filename:, (*51)

$upload->filename('my%$crazy@filename.txt')->sanitizeFilename();
move()

Moves the file to it's destination and returns the upload path., (*52)

$uploadPath = $upload->move();, (*53)

You can also use upload() which is an alias of move() if you feel the wording is more appropriate:, (*54)

$uploadPath = $upload->upload();, (*55)

That's it!, (*56)

The Versions

29/03 2017

dev-master

9999999-dev

Web Form File Uploader for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Craig Humphreys

29/03 2017

v1.1.1

1.1.1.0

Web Form File Uploader for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Craig Humphreys

16/12 2015

v1.1.0

1.1.0.0

Web Form File Uploader for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Craig Humphreys

16/12 2015

dev-dev

dev-dev

Web Form File Uploader for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Craig Humphreys

17/09 2015

v1.0.0

1.0.0.0

Web Form File Uploader for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Craig Humphreys