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.

7 calls to drush_download_file()
drush_hook_pre_pm_enable in docs/drush.api.php
Automatically download project dependencies at pm-enable time. Use a pre-pm_enable hook to download before your module is enabled, or a post-pm_enable hook (drush_hook_post_pm_enable) to run after your module is enabled.
drush_lib_fetch in includes/drush.inc
Download and extract a tarball to the lib directory.
drush_xkcd_display in examples/xkcd.drush.inc
Retrieve and display a table of metadata for an XKCD cartoon, then retrieve and display the cartoon using a specified image viewer.
package_handler_download_project in commands/pm/package_handler/wget.inc
Download a project.
release_info_print_releasenotes in commands/pm/release_info/updatexml.inc
Prints release notes for given projects.

... See full list

File

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

Code

function drush_download_file($url, $destination = FALSE, $cache_duration = 0) {
  // Generate destination of omitted.
  if (!$destination) {
    $file = basename(current(explode('?', $url, 2)));
    $destination = getcwd() . '/' . basename($file);
  }

  if (drush_get_option('cache') && $cache_duration !== 0 && $cache_dir = drush_directory_cache('download')) {
    $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)));
    }
    else {
      if (_drush_download_file($url, $cache_file, TRUE)) {
        // Cache was set just by downloading file to right location.
      }
      elseif (file_exists($cache_file)) {
        drush_log(dt('!name retrieved from an expired cache since refresh failed.', array('!name' => $cache_name)), 'warning');
      }
      else {
        $cache_file = FALSE;
      }
    }

    if ($cache_file && copy($cache_file, $destination)) {
      // Copy cached file to the destination
      return $destination;
    }
  }
  elseif ($return = _drush_download_file($url, $destination)) {
    drush_register_file_for_deletion($return);
    return $return;
  }

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