You are here

mimedetect_fileupload.module in MimeDetect 8

Same filename and directory in other branches
  1. 7 mimedetect_fileupload/mimedetect_fileupload.module

MimeDetect file upload validation sub-module.

Validates all file upload for consistency between its real file content and the filename extension.

File

mimedetect_fileupload/mimedetect_fileupload.module
View source
<?php

/**
 * @file
 * MimeDetect file upload validation sub-module.
 *
 * Validates all file upload for consistency between its real file content and
 * the filename extension.
 */
use Drupal\file\FileInterface;

/**
 * Implements hook_file_validate().
 */
function mimedetect_fileupload_file_validate(FileInterface $file) {
  $errors = [];
  $detected_mime = \Drupal::service('mimedetect')
    ->getMime($file);
  if ($file
    ->getMimeType() != $detected_mime) {
    $extension = pathinfo($file
      ->getFilename(), PATHINFO_EXTENSION);
    $errors[] = t('Detected file MIME type %type does not match the filename extension %extension.', [
      '%type' => $detected_mime,
      '%extension' => $extension,
    ]);
  }
  return $errors;
}

Functions