1. drupal
    1. 8
    2. 7
7 – 8 file_save(stdClass $file)

Save 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

▾ 12 functions call file_save()

EntityCrudHookTestCase::testFileHooks in modules/simpletest/tests/entity_crud_hook_test.test
Test hook invocations for CRUD operations on files.
FileSaveTest::testFileSave in modules/simpletest/tests/file.test
file_copy in includes/
Copies a file to a new location and adds a file record to the database.
file_field_presave in modules/file/
Implements hook_field_presave().
file_move in includes/
Move a file to a new location and update the file's database entry.
file_save_data in includes/
Save a string to the specified destination and create a database file entry.
file_save_upload in includes/
Saves a file upload to a new location.
form_example_tutorial_10_submit in examples/form_example/
Submit handler for form_example_tutorial_10().
image_example_style_form_submit in examples/image_example/
Form Builder; Display a form for uploading an image.
image_field_update_field in modules/image/image.module
Implements hook_field_update_field().
user_save in modules/user/user.module
Save changes to a user account or add a new user.
user_update_7012 in modules/user/user.install
Add the user's pictures to the {file_managed} table and make them managed files.


includes/, line 571

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');

  return $file;