bitshost / upmvc-files-pack
Private file storage, MIME/size validation, and HMAC signed downloads for upMVC (works standalone or with upmvc-saas-pack; never stores uploads under public/).
v0.1.0
2026-11-28 19:41 UTC
Requires
- php: >=8.3
- ext-fileinfo: *
- bitshost/upmvc: ^2.8
Requires (Dev)
- phpstan/phpstan: ^2.2
- phpunit/phpunit: ^11.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Private file storage, real MIME/size validation, and HMAC signed downloads for any upMVC (^2.8) application — not SaaS-only.
Works standalone AND in SaaS:
bitshost/upmvc-files-packdepends only onbitshost/upmvc(^2.8) andext-fileinfo. It does not requirebitshost/upmvc-saas-packand registers no HTTP routes. Use it in single-tenant apps (private docs, camera frames, exports) or in multi-tenantupMVC-SaaSapps (with automatict_<tenantId>/key prefix isolation).
- Private by default — stores files under
storage/(neverpublic/), with atomic writes and strict path-traversal rejection. - Real MIME detection — inspects actual file bytes via
finfo(never trusts$_FILES['type']) and blocks executable extensions (.php,.phtml,.phar,.svg,.html,.sh, etc.) by default. - Stateless HMAC-SHA256 signed download tokens — short-lived, tamper-proof tokens verified with
hash_equals. - Swappable storage adapter —
StorageDriverInterfaceships withLocalStorageDriverand is ready for S3/R2 adapters later. - Decoupled via
upMVC 2.8Events— emitsfiles.storedandfiles.deleted.
Install
composer require bitshost/upmvc-files-pack
Usage
1. Store an uploaded file safely
use BitsHost\UpmvcFiles\FilePolicy; use BitsHost\UpmvcFiles\FileVault; use BitsHost\UpmvcFiles\LocalStorageDriver; $vault = new FileVault( driver: new LocalStorageDriver(dirname(__DIR__) . '/storage/uploads'), policy: new FilePolicy(maxBytes: 5 * 1024 * 1024), ); // Standalone (tenantId: null) or SaaS (tenantId: (int) $user['tenant_id']) $stored = $vault->storeUpload($_FILES['document'], tenantId: null, prefix: 'invoices'); // Save $stored->key, $stored->originalName, $stored->mimeType, $stored->sha256 in your DB
2. Issue & verify an expiring download token
use BitsHost\UpmvcFiles\SignedDownloadToken; $token = $vault->issueDownloadToken($stored->key, $appKey, ttlSeconds: 300, tenantId: 7); // In your download controller: $claim = SignedDownloadToken::verify($token, $appKey); if ($claim === null) { http_response_code(403); exit; } $bytes = $vault->read($claim['key'], expectedTenantId: $claim['tenant_id']);
Local development
composer install
composer check # PHPStan level 5 + PHPUnit