I have manually added some files to the Private Files directory on my Drupal 7 site and wish to add them to the file_managed table in the database.

This is what I have attempted:

  1. Load the existing file with the file_get_contents($PATH_TO_FILE) function.
  2. Used the file_save_data($loaded_file, $destination, FILE_EXISTING_REPLACE) function save the file and add it to the Database.

The Result:

  1. The expected file is in the File System but is 0 bytes and blank
  2. File is added to Database

The main issue is that the existing file isn't getting uploaded properly and is being displayed blank. How can I fix this?

1 Answer 1


Make sure that your $loaded_file is a file object enter link description here not just a file content.

  • That did it for me! I was attempting to pass a path to the file. As you suggested, creating a new file object and filling it with relevant info and then saving it that way did it for me. Thank you! Commented May 10, 2018 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.