function update_manager_file_get

Same name in other branches
  1. 7.x modules/update/update.manager.inc \update_manager_file_get()
  2. 9 core/modules/update/update.manager.inc \update_manager_file_get()
  3. 8.9.x core/modules/update/update.manager.inc \update_manager_file_get()
  4. 10 core/modules/update/update.manager.inc \update_manager_file_get()

Copies a file from the specified URL to the temporary directory for updates.

Returns the local path if the file has already been downloaded.

Parameters

$url: The URL of the file on the server.

Return value

string|false Path to local file, or FALSE if it could not be retrieved.

2 calls to update_manager_file_get()
UpdateManagerInstall::submitForm in core/modules/update/src/Form/UpdateManagerInstall.php
Form submission handler.
update_manager_batch_project_get in core/modules/update/update.manager.inc
Implements callback_batch_operation().

File

core/modules/update/update.manager.inc, line 219

Code

function update_manager_file_get($url) {
    $parsed_url = parse_url($url);
    $remote_schemes = [
        'http',
        'https',
        'ftp',
        'ftps',
        'smb',
        'nfs',
    ];
    if (!isset($parsed_url['scheme']) || !in_array($parsed_url['scheme'], $remote_schemes)) {
        // This is a local file, just return the path.
        return \Drupal::service('file_system')->realpath($url);
    }
    // Check the cache and download the file if needed.
    $cache_directory = _update_manager_cache_directory();
    $local = $cache_directory . '/' . \Drupal::service('file_system')->basename($parsed_url['path']);
    if (!file_exists($local) || update_delete_file_if_stale($local)) {
        try {
            $data = (string) \Drupal::httpClient()->get($url)
                ->getBody();
            return \Drupal::service('file_system')->saveData($data, $local, FileExists::Replace);
        } catch (ClientExceptionInterface $exception) {
            \Drupal::messenger()->addError(t('Failed to fetch file due to error "%error"', [
                '%error' => $exception->getMessage(),
            ]));
        } catch (FileException|InvalidStreamWrapperException $e) {
            \Drupal::messenger()->addError(t('Failed to save file due to error "%error"', [
                '%error' => $e->getMessage(),
            ]));
        }
        return FALSE;
    }
    else {
        return $local;
    }
}

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