function file_validate_extensions

7.x file.inc file_validate_extensions(stdClass $file, $extensions)
6.x file.inc file_validate_extensions($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-7.x/modules/simpletest/tests/file.test
Test the file_validate_extensions() function.
hook_file_insert in drupal-7.x/modules/system/system.api.php
Respond to a file being added.
10 string references to 'file_validate_extensions'
aggregator_form_opml_submit in drupal-7.x/modules/aggregator/aggregator.admin.inc
Form submission handler for aggregator_form_opml().
file_field_widget_upload_validators in drupal-7.x/modules/file/file.field.inc
Retrieves the upload validators for a file field.
file_managed_file_process in drupal-7.x/modules/file/file.module
Process function to expand the managed_file element type.
file_save_upload in drupal-7.x/includes/file.inc
Saves a file upload to a new location.
image_field_widget_form in drupal-7.x/modules/image/image.field.inc
Implements hook_field_widget_form().

... See full list

File

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