function hook_file_insert

Respond to a file being added.

This hook is called after a file has been added to the database. The hook doesn't distinguish between files created as a result of a copy or those created by an upload.

Parameters

$file: The file that has been added.

See also

file_save()

Related topics

4 functions implement hook_file_insert()

Note: the procedural functions in this list are found by pattern matching, so the list may include some functions that are not actually implementations of this hook.

EntityCrudHookTestHooks::fileInsert in core/modules/system/tests/modules/entity_crud_hook_test/src/Hook/EntityCrudHookTestHooks.php
Implements hook_ENTITY_TYPE_insert() for file entities.
entity_crud_hook_test_file_insert in modules/simpletest/tests/entity_crud_hook_test.module
Implements hook_file_insert().
FileTestHooks::fileInsert in core/modules/file/tests/file_test/src/Hook/FileTestHooks.php
Implements hook_ENTITY_TYPE_insert() for file entities.
file_test_file_insert in modules/simpletest/tests/file_test.module
Implements hook_file_insert().
1 invocation of hook_file_insert()
file_save in includes/file.inc
Saves a file object to the database.

File

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

Code

function hook_file_insert($file) {
  // Add a message to the log, if the file is a jpg
  $validate = file_validate_extensions($file, 'jpg');
  if (empty($validate)) {
    watchdog('file', 'A jpg has been added.');
  }
}

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