A media library should manage more than files in an uploads directory. It needs metadata, ownership, validation, derivative generation, stable references, and a safe delivery strategy.
Treat uploads as untrusted input
Validate file size, extension, detected MIME type, and image dimensions. Do not trust the browser-provided filename or content type. Generate server-side names and store the original name only as display metadata.
Keep uploaded files outside executable application paths where possible. If public delivery is required, serve files through a controlled location or handler with safe response headers. Never allow uploaded PHP or other executable content to run.
Store metadata separately
A media record can include storage key, detected type, byte size, dimensions, alternative text, caption, uploader, checksum, and timestamps. Content should reference the media record rather than embedding a physical filesystem path.
This abstraction allows storage to move later without rewriting every article.
Generate derivatives deliberately
Create thumbnails and responsive image variants from validated originals. Limit pixel dimensions and processing time to reduce decompression-bomb risk. Preserve the original when editorial policy requires it, and make derivative generation retryable.
Manage lifecycle and references
Before deleting an asset, check whether content records reference it. Prefer a soft-delete or review queue over immediate destructive deletion. Backups must include both media objects and their metadata.
Implementation checklist
- Validate content, size, and dimensions server-side.
- Generate opaque storage names.
- Keep execution disabled in upload storage.
- Store metadata and content references separately.
- Check references before deletion.