enjoys / upload
Installs: 1 199
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 1
Requires
- php: ^8.0
- league/flysystem: ^3.0
- psr/http-message: ^1.0 | ^2.0
Requires (Dev)
- guzzlehttp/psr7: ^2.4
- infection/infection: ^0.26
- league/flysystem-memory: ^3.0
- phpunit/phpunit: ^9.5
- vimeo/psalm: ^4.24
README
File uploads library with validation uses PSR-7 UploadedFileInterface and League\Flysystem as a file storage library
Installing
This package requires PHP version 8.0 or later.
This package is available via Composer:
composer require enjoys/upload:^3.0
Usage
use Psr\Http\Message\ServerRequestInterface; /** @var Psr\Http\Message\UploadedFileInterface $uploadedFile */ /** @var League\Flysystem\Filesystem $filesystem */ $file = new Enjoys\Upload\UploadProcessing($uploadedFile, $filesystem); try { $file->upload(); }catch (\Exception $e){ // handle exception }
Validation
Currently there are 3 validation rules, but if a specific validation is needed, you can write a rule by implementing the Enjoys\Upload\RuleInterface interface:
- Extension (Enjoys\Upload\Rule\Extension)
- Size (Enjoys\Upload\Rule\Size)
- MediaType (Enjoys\Upload\Rule\MediaType)
/** @var Enjoys\Upload\UploadProcessing $file */ /** @var Enjoys\Upload\RuleInterface $rule */ // ... set rule before called $file->upload() $file->addRule($rule); $file->upload();
Extension Rule
Allowed extension case-insensitive
$rule = new Enjoys\Upload\Rule\Extension(); $rule->allow('png'); // or $rule->allow('png, jpg'); // or $rule->allow(['png','jpg']);
Size Rule
$rule = new Enjoys\Upload\Rule\Size(); $rule->setMaxSize(10*1024*1024) ->setMinSize(1*1024*1024); // in bytes
MediaType Rule
$rule = new Enjoys\Upload\Rule\MediaType(); $rule->allow('image/*') ->allow('application/pdf') // ... ;
Methods
Enjoys\Upload\UploadProcessing::class
setFilename(filename: string)
Set new filename for uploaded file. Called before upload.
/** @var Enjoys\Upload\UploadProcessing $file */ $file->setFilename('name');
addRule(rule: Enjoys\Upload\RuleInterface)
Called before upload.
/** @var Enjoys\Upload\UploadProcessing $file */ /** @var Enjoys\Upload\RuleInterface $rule */ $file->addRule($rule);
addRules(rules: Enjoys\Upload\RuleInterface[])
Called before upload.
/** @var Enjoys\Upload\UploadProcessing $file */ /** @var Enjoys\Upload\RuleInterface[] $rules */ $file->addRules($rules);
upload(targetPath: string)
/** @var Enjoys\Upload\UploadProcessing $file */ $file->upload(); // $file->upload('sub_directory');
getTargetPath()
Called after upload. Something like a location in the file system is returned. If called before upload, returns null
.
/** @var Enjoys\Upload\UploadProcessing $file */ $file->getTargetPath(); // return null or string
getFilesystem()
Returns League\Flysystem\Filesystem::class
/** @var Enjoys\Upload\UploadProcessing $file */ $file->getFilesystem();
getUploadedFile()
Returns Psr\Http\Message\UploadedFileInterface::class
/** @var Enjoys\Upload\UploadProcessing $file */ $file->getUploadedFile();
getFileInfo()
Returns Enjoys\Upload\FileInfo::class
/** @var Enjoys\Upload\UploadProcessing $file */ $file->getFileInfo();
Enjoys\Upload\FileInfo::class
getFilename()
Returns full filename, ex. new_file_name.jpg
/** @var Enjoys\Upload\FileInfo $fileInfo */ $fileInfo->getFilename();
getOriginalFilename()
Returns original filename, ex. original_file_name.jpg
/** @var Enjoys\Upload\FileInfo $fileInfo */ $fileInfo->getOriginalFilename();
getFilenameWithoutExtension()
Returns filename without extension, ex. new_file_name
/** @var Enjoys\Upload\FileInfo $fileInfo */ $fileInfo->getFilenameWithoutExtension();
getExtension()
Returns extension, ex. jpg
/** @var Enjoys\Upload\FileInfo $fileInfo */ $fileInfo->getExtension();
getExtensionWithDot()
Returns extension with dot before, ex. .jpg
/** @var Enjoys\Upload\FileInfo $fileInfo */ $fileInfo->getExtensionWithDot();
getSize()
Returns filesize in bytes, ex. 102435
/** @var Enjoys\Upload\FileInfo $fileInfo */ $fileInfo->getSize();
getMediaType()
Returns media type, determine by client extension, ex. image/jpg
/** @var Enjoys\Upload\FileInfo $fileInfo */ $fileInfo->getMediaType();