file_validate_extensions

Versions
6
file_validate_extensions($file, $extensions)
7
file_validate_extensions(stdClass $file, $extensions)

Check that the filename ends with an allowed extension.

See also

hook_file_validate()

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.

Related topics

Code

includes/file.inc, line 1289

<?php
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;
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.