function file_validate_extensions

7.x file_validate_extensions(stdClass $file, $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.
10 string references to 'file_validate_extensions'
aggregator_form_opml_submit in drupal/modules/aggregator/
Form submission handler for aggregator_form_opml().
file_field_widget_upload_validators in drupal/modules/file/
Retrieves the upload validators for a file field.
file_managed_file_process in drupal/modules/file/file.module
Process function to expand the managed_file element type.
file_save_upload in drupal/includes/
Saves a file upload to a new location.
image_field_widget_form in drupal/modules/image/
Implements hook_field_widget_form().

... See full list


drupal/includes/, line 1676
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;