Same name and namespace in other branches
  1. 6.x includes/file.inc \file_validate_extensions()
  2. 8.9.x core/modules/file/file.module \file_validate_extensions()
  3. 9 core/modules/file/file.module \file_validate_extensions()

Checks that the filename ends with an allowed extension.

Parameters

$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

hook_file_validate()

Related topics

3 calls to file_validate_extensions()
FileValidatorTest::testFileValidateExtensions in modules/simpletest/tests/file.test
Test the file_validate_extensions() function.
file_save_upload in includes/file.inc
Saves a file upload to a new location.
hook_file_insert in modules/system/system.api.php
Respond to a file being added.
8 string references to 'file_validate_extensions'
file_field_widget_upload_validators in modules/file/file.field.inc
Retrieves the upload validators for a file field.
file_managed_file_process in modules/file/file.module
Process function to expand the managed_file element type.
file_save_upload in includes/file.inc
Saves a file upload to a new location.
image_field_widget_form in modules/image/image.field.inc
Implements hook_field_widget_form().
locale_translate_import_form_submit in modules/locale/locale.admin.inc
Process the locale import form submission.

... See full list

File

includes/file.inc, line 1805
API for handling file uploads and server file management.

Code

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;
}