O'Reilly logo

Drupal 8 Module Development by Daniel Sipos

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required