function file_validate_extensions

Checks that the filename ends with an allowed extension.


$file: A Drupal file object.

$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


Related topics

2 calls to file_validate_extensions()
FileValidatorTest::testFileValidateExtensions in drupal/modules/simpletest/tests/file.test
Test the file_validate_extensions() function.
hook_file_insert in drupal/modules/system/system.api.php
Respond to a file being added.


drupal/includes/, line 1745
API for handling file uploads and server file management.


function file_validate_extensions(stdClass $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;