system_retrieve_file

Versions
mediamosa-21
system_retrieve_file($url, $destination = NULL, $managed = FALSE, $replace = FILE_EXISTS_RENAME)

Attempts to get a file using drupal_http_request and to store it locally.

Parameters

$url The URL of the file to grab.

$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://".

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

$replace boolean 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

On success the location the file was saved to, FALSE on failure.

▾ 1 function calls system_retrieve_file()

update_manager_file_get in modules/update/update.manager.inc
Copies a file from $url to the temporary directory for updates.

Code

modules/system/system.module, line 3179

<?php
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(basename($parsed_url['path']));
  }
  else {
    if (is_dir(drupal_realpath($destination))) {
      // Prevent URIs with triple slashes when glueing parts together.
      $path = str_replace('///', '//', "$destination/") . basename($parsed_url['path']);
    }
    else {
      $path = $destination;
    }
  }
  $result = drupal_http_request($url);
  if ($result->code != 200) {
    drupal_set_message(t('HTTP error @errorcode occured when trying to fetch @remote.', array('@errorcode' => $result->code, '@remote' => $url)), 'error');
    return FALSE;
  }
  $local = $managed ? file_save_data($result->data, $path, $replace) : file_unmanaged_save_data($result->data, $path, $replace);
  if (!$local) {
    drupal_set_message(t('@remote could not be saved to @path.', array('@remote' => $url, '@path' => $path)), 'error');
  }

  return $local;
}
?>