function install_retrieve_file

Same name and namespace in other branches
  1. 9 core/includes/install.core.inc \install_retrieve_file()
  2. 8.9.x core/includes/install.core.inc \install_retrieve_file()
  3. 10 core/includes/install.core.inc \install_retrieve_file()

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

Parameters

string $uri: The URI 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.

Return value

bool TRUE on success, FALSE on failure.

1 call to install_retrieve_file()
install_check_translations in core/includes/install.core.inc
Checks installation requirements and reports any errors.

File

core/includes/install.core.inc, line 1442

Code

function install_retrieve_file($uri, $destination) {
    $parsed_url = parse_url($uri);
    
    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    if (is_dir($file_system->realpath($destination))) {
        // Prevent URIs with triple slashes when gluing parts together.
        $path = str_replace('///', '//', "{$destination}/") . $file_system->basename($parsed_url['path']);
    }
    else {
        $path = $destination;
    }
    try {
        $response = \Drupal::httpClient()->get($uri, [
            'headers' => [
                'Accept' => 'text/plain',
            ],
        ]);
        $data = (string) $response->getBody();
        if (empty($data)) {
            return FALSE;
        }
    } catch (ClientExceptionInterface) {
        return FALSE;
    }
    return file_put_contents($path, $data) !== FALSE;
}

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