1. Go to this page and download the library: Download phatnt93/php-upload-helper library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
phatnt93 / php-upload-helper example snippets
use UploadHelper\Upload;
// Process a single file from $_FILES['avatar']
$result = Upload::file($_FILES['avatar'])
->allowedExtensions(['jpg', 'jpeg', 'png', 'webp'])
->maxSize('5M')
->slugName()
->resize(400, 400, mode: 'crop')
->toWebp(quality: 85)
->saveTo('/var/www/uploads/avatars');
if ($result->isSuccess()) {
echo "Saved to: " . $result->getFilePath();
echo "Filename: " . $result->getFilename();
echo "File size: " . $result->getFormattedSize();
} else {
echo "Upload failed: " . $result->getErrorMessage();
}
use UploadHelper\Upload;
// Feed the raw $_FILES array directly:
$batch = Upload::files($_FILES['photos'])
->allowedExtensions(['jpg', 'png', 'webp'])
->maxSize('10M')
->each(function ($builder, $index) {
$builder->randomName(16)
->resize(1200, 800, mode: 'fit')
->watermarkImage('/path/to/watermark.png', position: 'bottom-right', opacity: 70)
->saveTo('/var/www/uploads/gallery');
})
->rollbackOnError(true) // Automatically deletes saved files if any file in the batch fails
->process();
if ($batch->allSuccessful()) {
echo "All " . count($batch->getSuccessful()) . " files uploaded successfully!";
foreach ($batch->getSuccessful() as $file) {
echo "- " . $file->getFilename() . " (" . $file->getFormattedSize() . ")\n";
}
} else {
echo "Batch failed with " . count($batch->getFailed()) . " error(s):\n";
foreach ($batch->getFailed() as $failed) {
echo "- " . $failed->getOriginalName() . ": " . $failed->getErrorMessage() . "\n";
}
}
// Standard Web Form Input
$builder = Upload::file($_FILES['avatar']); // Single $_FILES element
$batch = Upload::files($_FILES['photos']); // Multi-file $_FILES element
$batch = Upload::fromGlobals(); // All $_FILES at once
// Alternative Sources
$builder = Upload::fromPath('/path/to/local/file.jpg');
$builder = Upload::fromBase64('data:image/png;base64,iVBORw0KGgo...', 'avatar.png');
$builder = Upload::fromUrl('https://example.com/asset.png', timeout: 15, maxDownloadBytes: 52_428_800);
$builder = Upload::fromPsr7($psr7UploadedFile);
$builder->grayscale(); // Convert to black and white
$builder->brightness(20); // Brightness: -255 to +255
$builder->contrast(-10); // Contrast: -100 to +100
$builder->blur(); // Gaussian blur
$builder->sharpen(); // Unsharp mask filter
$builder->invert(); // Invert image colors
$result = Upload::file($_FILES['photo'])->saveTo('/uploads');
if ($result->isSuccess()) {
$file = $result->getFile(); // Instance of ProcessedFile
echo $file->getOriginalName(); // e.g. "My Picture.png"
echo $file->getFilename(); // e.g. "my-picture.webp"
echo $file->getSavedPath(); // e.g. "/uploads/my-picture.webp"
echo $file->getFilePath(); // Alias to getSavedPath()
echo $file->getMime(); // e.g. "image/webp"
echo $file->getExtension(); // e.g. "webp"
echo $file->getSize(); // Size in bytes
echo $file->getFormattedSize(); // e.g. "1.42 MB"
echo $file->getWidth(); // e.g. 1200
echo $file->getHeight(); // e.g. 800
print_r($file->getDimensions()); // ['width' => 1200, 'height' => 800]
print_r($file->toArray()); // Export array
} else {
echo $result->getErrorMessage(); // Translated error message
echo $result->getErrorCode(); // e.g. "ERR_FILE_TOO_LARGE"
print_r($result->getErrors()); // ['code' => ..., 'message' => ..., 'context' => ...]
// Technical detail for logs only (English, may contain paths) - never show it to end users:
error_log($result->getErrors()['context']['reason'] ?? '');
}
$batch = Upload::files($_FILES['photos'])
->saveTo('/uploads/gallery')
->rollbackOnError(true)
->process();
$batch->allSuccessful(); // Returns true if all files succeeded
$batch->hasFailures(); // Returns true if one or more files failed
$batch->count(); // Total processed files count
// Access lists:
$batch->getSuccessful(); // Array of ProcessedFile objects
$batch->getFailed(); // Array of SingleResult error objects
$batch->getErrors(); // Array of error details keyed by original name