file_save

Definition

file_save($file)
drupal/includes/file.inc, line 331

Description

Save a file object to the database.

If the $file->fid is not set a new record will be added. Re-saving an existing file will not change its status.

@see hook_file_insert() @see hook_file_update()

Parameters

$file A file object returned by file_load().

Return value

The updated file object.

Related topics

Namesort iconDescription
File interfaceCommon file handling functions.

Code

function file_save($file) {
  $file = (object)$file;
  $file->timestamp = REQUEST_TIME;
  $file->filesize = filesize($file->filepath);

  if (empty($file->fid)) {
    drupal_write_record('files', $file);
    // Inform modules about the newly added file.
    module_invoke_all('file_insert', $file);
  }
  else {
    drupal_write_record('files', $file, 'fid');
    // Inform modules that the file has been updated.
    module_invoke_all('file_update', $file);
  }

  return $file;
}