vista-php / upload
Upload files library
Requires
- php: ^8.3
- ext-fileinfo: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.68
- mockery/mockery: ^1.6
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.5
README
Introduction
Vista PHP Upload module is a part of Vista PHP Framework, but can also be used as a stand alone file manipulation package for PHP. It is designed to simplify file uploads and file manipulation in PHP.
Getting Started
Installation
composer require vista-php/upload
Usage
All you need to do is instantiate the Vista\Upload\FileUploader
class and call the upload
method.
Constructor of the FileUploader class takes one parameter:
string $name
- The name of the file input field in the form.
Example:
use Vista\Upload\FileUploader; $uploader = new FileUploader('file'); $uploader->upload();
Configuration
You can configure the allowed mime types, maximum file size, upload directory, allow overwrite, file name, file nameprefix, file name suffix, allowed file types and perform manual validation based on the configured criteria.
Example:
use Vista\Upload\FileUploader; $uploader = new FileUploader('file'); $uploader->setAllowedMimeTypes(['image/jpeg', 'image/png', 'image/gif']) ->setMaxFileSize(1024 * 1024); // 1MB ->setUploadDirectory(__DIR__ . '/uploads') // It should be a writable directory ->setAllowOverwrite(true) // default is false ->setFileName('file') ->setFileNamePrefix('prefix_') ->setFileNameSuffix('_suffix.jpg'); $uploader->upload();
If you need manual validation based on defined criteria without actually uploading the file, you can call:
$validation = $uploader->validate(); // true or false
Deleting Files
You can delete files using the delete
method. It takes the file path as a parameter.
use Vista\Upload\FileUploader; $uploader = new FileUploader(); $success = $uploader->delete(__DIR__ '/path/to/file.jpg'); // returns true or false
Additional Methods
getResults
- Returns the upload results.
[
[
'filename' => 'file.jpg',
'destination' => '/path/to/uploads/file.jpg',
]
]
getErrors
- Returns the upload errors.
[
'File type not allowed text/plain',
]
getOriginalFilename
- Returns the original file name.
file.txt
getOriginalExtension
- Returns the original file extension.
txt