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.

6 calls to drush_download_file()
drush_hook_pre_pm_enable in ./drush.api.php
Automatically download project dependencies at pm-enable time.
drush_xkcd_display in examples/xkcd.drush.inc
Display a given XKCD cartoon.
package_handler_download_project in commands/pm/package_handler/wget.inc
Download a project.
Project::getInstance in lib/Drush/UpdateService/Project.php
Downloads release info xml from update service.
Project::getReleaseNotes in lib/Drush/UpdateService/Project.php
Prints release notes for given projects.

... See full list

File

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

Code

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

  // Simply copy local files to the destination
  if (!_drush_is_url($url)) {
    return copy($url, $destination) ? $destination : FALSE;
  }

  if ($cache_duration !== 0 && $cache_file = drush_download_file_name($url)) {
    // 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_file)));
    }
    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_file)), LogLevel::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;
}