You are here

function file_validate_extensions in Drupal 7

Same name and namespace in other branches
  1. 8 core/modules/file/file.module \file_validate_extensions()
  2. 6 includes/file.inc \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.

File

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