function file_validate_extensions

Same name in other branches
  1. 9 core/modules/file/file.module \file_validate_extensions()
  2. 8.9.x core/modules/file/file.module \file_validate_extensions()
  3. 10 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 1805

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.