8.2.x file.module file_validate_extensions(FileInterface $file, $extensions)
8.0.x file.module file_validate_extensions(FileInterface $file, $extensions)
8.1.x file.module file_validate_extensions(FileInterface $file, $extensions)
8.3.x file.module file_validate_extensions(FileInterface $file, $extensions)
6.x file.inc file_validate_extensions($file, $extensions)
7.x file.inc file_validate_extensions(stdClass $file, $extensions)

Check that the filename ends with an allowed extension. This check is not enforced for the user #1.


$file: A Drupal file object.

$extensions: A string with a space separated list of allowed file extensions, not including the period. For example, 'bmp jpg gif png'.

Return value

An array. If the file extension is not allowed, it will contain an error message.

Related topics

1 string reference to 'file_validate_extensions'
upload_node_form_submit in modules/upload/upload.module
Save new uploads and store them in the session to be associated to the node on upload_save.


includes/file.inc, line 743
API for handling file uploads and server file management.


function file_validate_extensions($file, $extensions) {
  global $user;

  $errors = array();

  // Bypass validation for uid  = 1.
  if ($user->uid != 1) {
    $regex = '/\.(' . @ereg_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;