function file_validate_extensions

7.x file.inc file_validate_extensions(stdClass $file, $extensions)
6.x file.inc file_validate_extensions($file, $extensions)
8.x file.module file_validate_extensions(FileInterface $file, $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

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/aggregator.admin.inc
Form submission handler for aggregator_form_opml().
file_field_widget_upload_validators in drupal/modules/file/file.field.inc
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/file.inc
Saves a file upload to a new location.
image_field_widget_form in drupal/modules/image/image.field.inc
Implements hook_field_widget_form().

... See full list

File

drupal/includes/file.inc, line 1719
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;
}