8.5.x system.module system_retrieve_file($url, $destination = NULL, $managed = FALSE, $replace = FILE_EXISTS_RENAME)
8.0.x system.module system_retrieve_file($url, $destination = NULL, $managed = FALSE, $replace = FILE_EXISTS_RENAME)
8.1.x system.module system_retrieve_file($url, $destination = NULL, $managed = FALSE, $replace = FILE_EXISTS_RENAME)
8.2.x system.module system_retrieve_file($url, $destination = NULL, $managed = FALSE, $replace = FILE_EXISTS_RENAME)
8.3.x system.module system_retrieve_file($url, $destination = NULL, $managed = FALSE, $replace = FILE_EXISTS_RENAME)
8.4.x system.module system_retrieve_file($url, $destination = NULL, $managed = FALSE, $replace = FILE_EXISTS_RENAME)
8.6.x system.module system_retrieve_file($url, $destination = NULL, $managed = FALSE, $replace = FILE_EXISTS_RENAME)
7.x system.module system_retrieve_file($url, $destination = NULL, $managed = FALSE, $replace = FILE_EXISTS_RENAME)

Attempts to get a file using Guzzle HTTP client and to store it locally.

Parameters

string $url: The URL of the file to grab.

string $destination: Stream wrapper URI specifying where the file should be placed. If a directory path is provided, the file is saved into that directory under its original name. If the path contains a filename as well, that one will be used instead. If this value is omitted, the site's default files scheme will be used, usually "public://".

bool $managed: If this is set to TRUE, the file API hooks will be invoked and the file is registered in the database.

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

  • FILE_EXISTS_REPLACE: Replace the existing file.
  • FILE_EXISTS_RENAME: Append _{incrementing number} until the filename is unique.
  • FILE_EXISTS_ERROR: Do nothing and return FALSE.

Return value

mixed One of these possibilities:

  • If it succeeds and $managed is FALSE, the location where the file was saved.
  • If it succeeds and $managed is TRUE, a \Drupal\file\FileInterface object which describes the file.
  • If it fails, FALSE.
2 calls to system_retrieve_file()
RetrieveFileTest::testFileRetrieving in core/modules/system/tests/src/Functional/System/RetrieveFileTest.php
Invokes system_retrieve_file() in several scenarios.
update_manager_file_get in core/modules/update/update.manager.inc
Copies a file from the specified URL to the temporary directory for updates.

File

core/modules/system/system.module, line 1443
Configuration system that lets administrators modify the workings of the site.

Code

function system_retrieve_file($url, $destination = NULL, $managed = FALSE, $replace = FILE_EXISTS_RENAME) {
  $parsed_url = parse_url($url);
  if (!isset($destination)) {
    $path = file_build_uri(drupal_basename($parsed_url['path']));
  }
  else {
    if (is_dir(\Drupal::service('file_system')
      ->realpath($destination))) {

      // Prevent URIs with triple slashes when glueing parts together.
      $path = str_replace('///', '//', "{$destination}/") . drupal_basename($parsed_url['path']);
    }
    else {
      $path = $destination;
    }
  }
  try {
    $data = (string) \Drupal::httpClient()
      ->get($url)
      ->getBody();
    $local = $managed ? file_save_data($data, $path, $replace) : file_unmanaged_save_data($data, $path, $replace);
  } catch (RequestException $exception) {
    drupal_set_message(t('Failed to fetch file due to error "%error"', [
      '%error' => $exception
        ->getMessage(),
    ]), 'error');
    return FALSE;
  }
  if (!$local) {
    drupal_set_message(t('@remote could not be saved to @path.', [
      '@remote' => $url,
      '@path' => $path,
    ]), 'error');
  }
  return $local;
}