function hook_file_validate

Same name in other branches
  1. 9 core/modules/file/file.api.php \hook_file_validate()
  2. 8.9.x core/modules/file/file.api.php \hook_file_validate()
  3. 10 core/modules/file/file.api.php \hook_file_validate()

Check that files meet a given criteria.

This hook lets modules perform additional validation on files. They're able to report a failure by returning one or more error messages.

Parameters

$file: The file object being validated.

Return value

An array of error messages. If there are no problems with the file return an empty array.

See also

file_validate()

Related topics

2 functions implement hook_file_validate()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

file_managed_file_validate in modules/file/file.module
An #element_validate callback for the managed_file element.
file_test_file_validate in modules/simpletest/tests/file_test.module
Implements hook_file_validate().
1 invocation of hook_file_validate()
file_validate in includes/file.inc
Checks that a file meets the criteria specified by the validators.

File

modules/system/system.api.php, line 2862

Code

function hook_file_validate($file) {
    $errors = array();
    if (empty($file->filename)) {
        $errors[] = t("The file's name is empty. Please give a name to the file.");
    }
    if (strlen($file->filename) > 255) {
        $errors[] = t("The file's name exceeds the 255 characters limit. Please rename the file and try again.");
    }
    return $errors;
}

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