Helpful functions for dealing with managed files

Apart from the staple file_save_data() function, we have a few other ones that can come in handy if we are dealing with managed files. Here's a few of them.

If we want to copy a file from one place to another while making sure a new database record is created, we can use file_copy(). It takes three parameters:

  • The FileInterface entity that needs to be copied
  • The destination URI where it should go
  • The flag indicating what to do in case a file with the same name exists

The parameters are the same as for file_save_data().

Apart from the actual copying, this function also invokes hook_file_copy() which allows modules to respond to files being copied.

Very similarly to file_copy(), we also have ...

Get Drupal 8 Module Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.