You are here

function file_validate_extensions in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/file/file.module \file_validate_extensions()

Checks that the filename ends with an allowed extension.

Parameters

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

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

Return value

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

See also

hook_file_validate()

1 call to file_validate_extensions()
ValidatorTest::testFileValidateExtensions in core/modules/file/src/Tests/ValidatorTest.php
Test the file_validate_extensions() function.

File

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

Code

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