system_retrieve_file

Versions
7
system_retrieve_file($url, $destination = NULL, $overwrite = TRUE)

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

Parameters

$url The URL of the file to grab.

$destination Where the file should be saved, if a directory is provided, file is saved in that directory with its original name. If a filename is provided, remote fileis stored to that location. NOTE: Relative to drupal "files" directory"

$overwrite boolean Defaults to TRUE, will overwrite existing files of the same name.

Return value

On success the address the files 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 3005

<?php
function system_retrieve_file($url, $destination = NULL, $overwrite = TRUE) {
  if (!$destination) {
    $destination = file_directory_path('temporary');
  }
  $parsed_url = parse_url($url);
  $local = is_dir(file_directory_path() . '/' . $destination) ? $destination . '/' . basename($parsed_url['path']) : $destination;

  if (!$overwrite && file_exists($local)) {
    drupal_set_message(t('@remote could not be saved. @local already exists', array('@remote' => $url, '@local' => $local)), 'error');
    return FALSE;
  }

  $result = drupal_http_request($url);
  if ($result->code != 200 || !file_save_data($result->data, $local)) {
    drupal_set_message(t('@remote could not be saved.', array('@remote' => $url)), 'error');
    return FALSE;
  }

  return $local;
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.