function file_save

Saves a file object to the database.

If the $file->fid is not set a new record will be added.


$file: A file object returned by file_load().

Return value

The updated file object.

See also



Related topics

13 calls to file_save()
EntityCrudHookTestCase::testFileHooks in drupal/modules/simpletest/tests/entity_crud_hook_test.test
Tests hook invocations for CRUD operations on files.
FileFieldTestCase::createTemporaryFile in drupal/modules/file/tests/file.test
Creates a temporary file, for a specific user.
FileSaveTest::testFileSave in drupal/modules/simpletest/tests/file.test
file_copy in drupal/includes/
Copies a file to a new location and adds a file record to the database.
file_field_presave in drupal/modules/file/
Implements hook_field_presave().

... See full list


drupal/includes/, line 612
API for handling file uploads and server file management.


function file_save(stdClass $file) {
  $file->timestamp = REQUEST_TIME;
  $file->filesize = filesize($file->uri);

  // Load the stored entity, if any.
  if (!empty($file->fid) && !isset($file->original)) {
    $file->original = entity_load_unchanged('file', $file->fid);
  module_invoke_all('file_presave', $file);
  module_invoke_all('entity_presave', $file, 'file');
  if (empty($file->fid)) {
    drupal_write_record('file_managed', $file);

    // Inform modules about the newly added file.
    module_invoke_all('file_insert', $file);
    module_invoke_all('entity_insert', $file, 'file');
  else {
    drupal_write_record('file_managed', $file, 'fid');

    // Inform modules that the file has been updated.
    module_invoke_all('file_update', $file);
    module_invoke_all('entity_update', $file, 'file');

  // Clear internal properties.

  // Clear the static loading cache.
  return $file;