function file_validate_extensions

Checks that the filename ends with an allowed extension.

Parameters

Drupal\file\File $file: A file entity.

$extensions: A string with a space separated list of allowed extensions.

Return value

An array. If the file extension is not allowed, it will contain an error message.

See also

hook_file_validate()

2 calls to file_validate_extensions()
hook_file_insert in drupal/core/modules/file/file.api.php
Respond to a file being added.
ValidatorTest::testFileValidateExtensions in drupal/core/modules/file/lib/Drupal/file/Tests/ValidatorTest.php
Test the file_validate_extensions() function.

File

drupal/core/modules/file/file.module, line 362
Defines a "managed_file" Form API field and a "file" field for Field module.

Code

function file_validate_extensions(File $file, $extensions) {
  $errors = array();
  $regex = '/\\.(' . preg_replace('/ +/', '|', preg_quote($extensions)) . ')$/i';
  if (!preg_match($regex, $file->filename)) {
    $errors[] = t('Only files with the following extensions are allowed: %files-allowed.', array(
      '%files-allowed' => $extensions,
    ));
  }
  return $errors;
}