Image Mime Validator
This module validates uploaded image files against a configurable list of allowed MIME types, helping to ensure only expected image formats are accepted. It offers flexible controls to block uploads, log violations, display user-facing errors, and send notification emails when disallowed image MIME types are detected.
The Image MIME Validator module validates uploaded image files against a configurable list of allowed MIME types. It helps ensure that only expected image formats are accepted and provides flexible controls to block uploads, log violations, display user-facing errors, and send notification emails when disallowed image MIME types are detected.
Features
- Allowed MIME list Configure a newline-separated list of allowed image MIME types (for example, image/jpeg, image/png).
- Image-only enforcement Validation is applied only when the detected MIME type starts with image/.
- Upload blocking Optionally block image uploads whose detected MIME type is not included in the allowed list.
- Watchdog logging Optionally log details of invalid image uploads (file information, file path, site URL, detected MIME type) to the Drupal log.
- End-user error messages Optionally display a detailed error message to the uploader when an invalid image upload is blocked.
- Email notifications Optionally send notification emails to a configurable list of recipients when an invalid image upload is detected.
- Per-language error text Configure localized error message text per language through the module’s configuration UI.
Installation
- Download and install the module as you would any other Drupal module
- Enable the module from the Extend page: /admin/modules
Configuration
- Navigate to the configuration page:
/admin/config/media/image-mime-validator - Configure the following settings:
- Allowed MIME types (allowed_mime_types)
- Newline-separated list of permitted image MIME types.
- Block invalid image uploads (image_mime_validator_block)
- When enabled, image uploads with disallowed MIME types are blocked.
- Log invalid image uploads (image_mime_validator_log)
- When enabled, invalid uploads are written to the Drupal log.
- Show error message on invalid uploads (image_mime_validator_show_error_message)
- Displays an error message to the uploader when an upload is blocked.
- Email on invalid uploads (image_mime_validator_email)
- Sends a notification email when an invalid image upload is detected.
- Notification recipients (image_mime_validator_notification_recipients)
- Comma-separated list of email addresses. Entries are validated for correct syntax.
- Error message per language (label.image_mime_validator_error_label.[langcode])
- Optional language-specific error message text.
Permissions
The module does not define custom permissions. It reuses existing file and media permissions provided by Drupal. Access to the configuration form is governed by standard Drupal configuration permissions.
Usage
- Enable and configure the module.
- The validator automatically runs on image uploads where the Image MIME Validator constraint is applied.
- If an uploaded file:
- Has a detected MIME type starting with image/, and
- Is not included in the allowed MIME list,
- then, based on configuration:
- The upload may be blocked.
- The incident may be logged.
- Notification emails may be sent.
- An error message may be shown to the uploader.
Technical Details
- Configuration storage
Settings are stored under image_mime_validator.settings. - Validation constraint
Implemented via:- ImageMimeValidationConstraint
- ImageMimeValidationConstraintValidator
- Validator behavior
- Resolves the real file path using file_system.realpath
- Detects the actual MIME type using mime_content_type()
- Applies validation only to MIME types beginning with image/
- Uses Drupal services for:
- Logging
- Messaging
- Language management
- Configuration
- Request handling
- Mail delivery