function drush_download_file

8.0.x drush.inc drush_download_file($url, $destination = FALSE, $cache_duration = 0)
6.x drush.inc drush_download_file($url, $destination = FALSE, $cache_duration = 0)
7.x drush.inc drush_download_file($url, $destination = FALSE, $cache_duration = 0)
4.x drush.inc drush_download_file($url, $destination = FALSE, $cache_duration = 0)
5.x drush.inc drush_download_file($url, $destination = FALSE, $cache_duration = 0)
master drush.inc drush_download_file($url, $destination = FALSE, $cache_duration = 0)

Download a file using wget, curl or file_get_contents, or via download cache.

Parameters

string $url: The url of the file to download.

string $destination: The name of the file to be saved, which may include the full path. Optional, if omitted the filename will be extracted from the url and the file downloaded to the current working directory (Drupal root if bootstrapped).

integer $cache_duration: The acceptable age of a cached file. If cached file is too old, a fetch will occur and cache will be updated. Optional, if ommitted the file will be fetched directly.

Return value

string The path to the downloaded file, or FALSE if the file could not be downloaded.

Related topics

1 call to drush_download_file()
_drush_pm_get_release_history_xml in commands/pm/pm.drush.inc
Download the release history xml for the specified request.

File

includes/drush.inc, line 1476
The drush API implementation and helpers.

Code

function drush_download_file($url, $destination = FALSE, $cache_duration = 0) {
  if (drush_get_option('cache') && $cache_duration !== 0 && $cache_dir = drush_directory_cache() . '/download') {
    drush_mkdir($cache_dir);
    $cache_name = str_replace(array(':', '/'), '-', $url);
    $cache_file = $cache_dir . "/" . $cache_name;
    // Check for cached, unexpired file.
    if (file_exists($cache_file) && filectime($cache_file) > ($_SERVER['REQUEST_TIME'] -$cache_duration)) {
      drush_log(dt('!name retrieved from cache.', array('!name' => $cache_name)));
      return $cache_file;
    }
    else {
      if (_drush_download_file($url, $cache_file, TRUE)) {
        // Cache was set just by downloading file to right location.
        return $cache_file;
      }
      elseif (file_exists($cache_file)) {
        drush_log(dt('!name retrieved from an expired cache since refresh failed.', array('!name' => $cache_name)), 'warning');
        return $cache_file;
      }
    }
  }
  elseif ($return = _drush_download_file($url, $destination)) {
    drush_register_file_for_deletion($return);
    return $return;
  }

  // Unable to retrieve from cache nor download.
  return FALSE;
}