Unmanaged file functions in file.inc have been deprecated and have been moved to the 'file_system' service and \Drupal\Core\File\FileSystemInterface
Methods on the interface now throw FileException or sub-classes if there are errors.
Before:
file_unmanaged_copy($source, $destination, $replace)
file_unmanaged_prepare($source, $destination, $replace)
file_unmanaged_move($source, $destination, $replace)
file_unmanaged_delete($path)
file_unmanaged_delete_recursive($path, $callback)
file_unmanaged_save_data($data, $destination, $replace)
file_prepare_directory($dir)
file_destination($destination, $replace)
file_create_filename($basename, $directory)
After:
<?php
try {
\Drupal::service('file_system')->copy($source, $destination, $replace);
\Drupal::service('file_system')->move($source, $destination, $replace);
\Drupal::service('file_system')->delete($path);
\Drupal::service('file_system')->deleteRecursive($path, $callback);
\Drupal::service('file_system')->saveData($data, $destination, $replace);
\Drupal::service('file_system')->prepareDirectory($directory, $options);
\Drupal::service('file_system')->getDestinationFilename($destination, $replace);
\Drupal::service('file_system')->createFilename($basename, $directory);
}
catch (\Drupal\Core\File\Exception\FileException $e) {
// Log or set message or doing something else.
}
?>
You can also inject the 'file_system' service into your class as \Drupal\Core\File\FileSystemInterface
In addition the following constants have been deprecated:
FILE_EXISTS_RENAME
FILE_EXISTS_REPLACE
FILE_EXISTS_ERROR
and replaced with
FileSystemInterface::EXISTS_RENAME
FileSystemInterface::EXISTS_REPLACE
FileSystemInterface::EXISTS_ERROR
The destination parameter of the methods FileSystem::copy(), FileSystem::move() and FileSystem::saveData() is now required, this is an API changed compared to 8.7.0-alpha2. 'public://'
or file_default_scheme() . '://'
can be used as a replacement to get the same behavior but it is discouraged to copy files directly into the top-level directory.
Comments
Nice and helpful
Nice and helpful documentation, thanks @kim.pepper.
Though it seems to be that the Core documentation is lacking these type of unstructured nodes. You may raise a request to merge document in Drupal core.
Just a suggestion, (Y)
While working with this nice
While working with this nice service I was very surprised that there is no readData or readFile method. A file system should be able to be read as well doesn't it?