function system_retrieve_file

Same name in other branches
  1. 7.x modules/system/system.module \system_retrieve_file()
  2. 9 core/modules/system/system.module \system_retrieve_file()
  3. 8.9.x core/modules/system/system.module \system_retrieve_file()

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:

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.

Deprecated

in drupal:10.2.0 and is removed from drupal:11.0.0. There is no replacement.

See also

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

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.
SystemFunctionsLegacyTest::testSystemRetrieveFile in core/modules/system/tests/src/Kernel/System/SystemFunctionsLegacyTest.php
@covers ::system_retrieve_file

File

core/modules/system/system.module, line 1116

Code

function system_retrieve_file($url, $destination = NULL, $managed = FALSE, $replace = FileSystemInterface::EXISTS_RENAME) {
    @trigger_error('system_retrieve_file is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3223362', E_USER_DEPRECATED);
    $parsed_url = parse_url($url);
    
    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    if (!isset($destination)) {
        $path = $file_system->basename($parsed_url['path']);
        $path = \Drupal::config('system.file')->get('default_scheme') . '://' . $path;
        $path = \Drupal::service('stream_wrapper_manager')->normalizeUri($path);
    }
    else {
        if (is_dir($file_system->realpath($destination))) {
            // Prevent URIs with triple slashes when glueing parts together.
            $path = str_replace('///', '//', "{$destination}/") . \Drupal::service('file_system')->basename($parsed_url['path']);
        }
        else {
            $path = $destination;
        }
    }
    try {
        $data = (string) \Drupal::httpClient()->get($url)
            ->getBody();
        if ($managed) {
            
            /** @var \Drupal\file\FileRepositoryInterface $file_repository */
            $file_repository = \Drupal::service('file.repository');
            $local = $file_repository->writeData($data, $path, $replace);
        }
        else {
            $local = $file_system->saveData($data, $path, $replace);
        }
    } catch (ClientExceptionInterface $exception) {
        \Drupal::messenger()->addError(t('Failed to fetch file due to error "%error"', [
            '%error' => $exception->getMessage(),
        ]));
        return FALSE;
    } catch (FileException|InvalidStreamWrapperException $e) {
        \Drupal::messenger()->addError(t('Failed to save file due to error "%error"', [
            '%error' => $e->getMessage(),
        ]));
        return FALSE;
    }
    if (!$local) {
        \Drupal::messenger()->addError(t('@remote could not be saved to @path.', [
            '@remote' => $url,
            '@path' => $path,
        ]));
    }
    return $local;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.