nedbb / secure-upload
A small, secure-by-default file upload library
v1.0.0
2026-11-26 13:51 UTC
Requires
- php: >=8.1
- ext-fileinfo: *
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-26 14:13:52 UTC
README
A small, zero-dependency, secure-by-default PHP file upload library implementing SPEC.md (v1.0).
Requirements
- PHP
>= 8.1 - PHP
fileinfoextension (ext-fileinfo)
Installation
Install with Composer:
composer require nedbb/secure-upload
Or require the two source files directly (no third-party dependencies):
require __DIR__ . '/src/UploadException.php'; require __DIR__ . '/src/Uploader.php';
Quick Start
Create a writable storage directory outside the web root with 0750 permissions:
mkdir -m 0750 /path/to/private_uploads
Then handle uploads with SecureUpload\Uploader:
<?php declare(strict_types=1); require __DIR__ . '/vendor/autoload.php'; use SecureUpload\Uploader; use SecureUpload\UploadException; try { $uploader = new Uploader([ 'storageDir' => '/path/to/private_uploads', // required, outside web root 'allowedTypes' => ['jpg', 'png', 'pdf'], 'maxFileSize' => 2 * 1024 * 1024, // 2 MB ]); $result = $uploader->handle($_FILES['photo'] ?? []); // $result = ['storedName' => '<32-hex>.jpg', 'size' => 12345, 'mime' => 'image/jpeg'] } catch (UploadException $e) { http_response_code(400); // Error codes never include file paths or system details. echo 'Upload failed: ' . htmlspecialchars($e->getErrorCode()); }
See example.php for a complete form + POST handler example.
Options
| Option | Default | Meaning |
|---|---|---|
storageDir |
(required) | Existing, writable folder where files are saved (throws CONFIG_ERROR if missing or not writable) |
allowedTypes |
['jpg', 'png'] |
Allowed extensions (supported: jpg, jpeg, png, gif, webp, pdf) |
maxFileSize |
5242880 (5 MB) |
Maximum accepted file size in bytes |
keepOriginalName |
false |
If true, includes a sanitized originalName in the returned metadata (never used for storage paths) |
requireHttpUpload |
true |
Enforces is_uploaded_file() / move_uploaded_file(). Set false only in automated tests |
Always-blocked extensions (php, phtml, phar, php5, jsp, asp, aspx, exe, sh, bat, htaccess) are rejected even if added to allowedTypes.
Security Properties
- Validation order: file size -> extension -> real content MIME via
finfo-> path containment -> atomic temp write + rename. - Random storage names:
32 random hex characters + "." + validated extension. User filenames are never used on disk. - Permissions: uploaded files are written with
0640permissions (never executable). - Safe errors: exceptions expose only the spec error codes and never leak paths or system details.
Error Codes
| Code | Meaning |
|---|---|
FILE_TOO_LARGE |
File exceeds maxFileSize (or PHP upload size limit) |
TYPE_NOT_ALLOWED |
Extension is missing, blocked, or not in allowedTypes |
MIME_MISMATCH |
Detected content type does not match the extension |
INVALID_PATH |
Final path is outside storageDir |
CONFIG_ERROR |
Missing or invalid uploader configuration |
UPLOAD_FAILED |
Upload transfer or file write failed |
Running Tests
php tests/run.php # or with Composer: composer test