uploader/uploader

Basic PHP library to upload files using different sources

v1.4.1 2016-07-07 17:39 UTC

This package is auto-updated.

Last update: 2024-04-05 17:48:11 UTC


README

Basic php library to manage uploaded files

Scrutinizer Code Quality

Created by Oscar Otero http://oscarotero.com oom@oscarotero.com

Basic usage

//Init a Uploader instance:
$uploader = new Uploader\Uploader('/base/path/to/uploads');

//Save an uploaded file:
$file = $uploader($_FILES['my-file']);

//Save a psr7 UploadedFileInterface instance
$file = $uploader($uploadedFile);

//Save a file from an url
$file = $uploader('http://example.com/files/file1.jpg');

//Save from base64 value
$file = $uploader('data:image/png;base64,...');

Advanced usage

//Init a Uploader instance:
$uploader = new Uploader\Uploader('/base/path/to/uploads');

//Add some configuration
$uploader
	->setPrefix(function () {
		return uniqid();
	})
	->setDirectory('images');

//Assign an input
$imageUpload = $uploader->with($_FILES['my-image']);

//Save
$imageUpload->save();

//Get the relative path
echo $imageUpload->getDestination();

API

The following methods configure how the uploaded file will be saved

Setter Getter Description
`setPrefix(string Closure)` getPrefix()
`setOverwrite(boolean Closure)` getOverwrite()
`setDestination(boolean Closure)` getDestination(bool $absolute = false)
`setDirectory(string Closure)` getDirectory()
`setFilename(string Closure)` getFilename()
`setExtension(string Closure)` getExtension()
`setCwd(string Closure)` getCwd()
`setCreateDir(boolean Closure)` getCreateDir()

Example:

$uploader = new Uploader\Uploader(__DIR__.'/my-uploads');

$upload = $uploader
	->with($_FILES['my-file'])
	->setPrefix(uniqid())
	->setOverwrite(true)
	->setCreateDir(true)
	->setDirectory('files')
	->setExtension(function ($upload) {
		return strtolower($upload->getExtension());
	});

try {
	$upload->save();

	echo 'The file has been saved in '.$upload->getDestination();
} catch (Exception $e) {
	echo $e->getMessage();
}