1 file.inc file_usage_add(File $file, $module, $type, $id, $count = 1)

Records that a module is using a file.


  • A module that associates files with nodes, so $type would be 'node' and $id would be the node's nid. Files for all revisions are stored within a single nid.
  • The User module associates an image with a user, so $type would be 'user' and the $id would be the user's uid.


File $file: A file entity.

$module: The name of the module using the file.

$type: The type of the object that contains the referenced file.

$id: The unique, numeric ID of the object containing the referenced file.

$count: (optional) The number of references to add to the object. Defaults to 1.

See also



Related topics


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


function file_usage_add(File $file, $module, $type, $id, $count = 1) {
      'fid' => $file->fid,
      'module' => $module,
      'type' => $type,
      'id' => $id,
    ->fields(array('count' => $count))
    ->expression('count', 'count + :count', array(':count' => $count))

  // Make sure that a used file is permanent.
  if ($file->status != FILE_STATUS_PERMANENT) {
    $file->status = FILE_STATUS_PERMANENT;