You are here

function file_unmanaged_save_data in Drupal 8

Same name and namespace in other branches
  1. 7 includes/file.inc \file_unmanaged_save_data()

Saves a file to the specified destination without invoking file API.

This function is identical to file_save_data() except the file will not be saved to the {file_managed} table and none of the file_* hooks will be called.

Parameters

$data: A string containing the contents of the file.

$destination: A string containing the destination location. This must be a stream wrapper URI. If no value is provided, a randomized name will be generated and the file will be saved using Drupal's default files scheme, usually "public://".

$replace: Replace behavior when the destination file already exists:

Return value

A string with the path of the resulting file, or FALSE on error.

Deprecated

in drupal:8.7.0 and is removed from drupal:9.0.0. Use \Drupal\Core\File\FileSystemInterface::saveData().

See also

file_save_data()

https://www.drupal.org/node/3006851

Related topics

1 call to file_unmanaged_save_data()
FileSystemDeprecationTest::testDeprecatedUnmanagedSaveData in core/tests/Drupal/KernelTests/Core/File/FileSystemDeprecationTest.php
@expectedDeprecation file_unmanaged_save_data() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\File\FileSystemInterface::saveData(). See https://www.drupal.org/node/3006851.

File

core/includes/file.inc, line 921
API for handling file uploads and server file management.

Code

function file_unmanaged_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
  @trigger_error('file_unmanaged_save_data() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \\Drupal\\Core\\File\\FileSystemInterface::saveData(). See https://www.drupal.org/node/3006851.', E_USER_DEPRECATED);
  try {

    // Build a destination URI if necessary.
    if (!isset($destination)) {
      $destination = file_default_scheme() . '://';
    }
    return \Drupal::service('file_system')
      ->saveData($data, $destination, $replace);
  } catch (FileWriteException $e) {
    \Drupal::messenger()
      ->addError(t('The file could not be created.'));
    return FALSE;
  } catch (FileException $e) {
    return FALSE;
  }
}