You are here

function download_file_direct_download in DownloadFile 7

Same name and namespace in other branches
  1. 6 download_file.module \download_file_direct_download()
  2. 7.3 download_file.module \download_file_direct_download()
  3. 7.2 download_file.module \download_file_direct_download()

Page who forces the download of the file by changing the header.

Parameters

$fid: The filefield identifier.

1 string reference to 'download_file_direct_download'
download_file_menu in ./download_file.module
Implementation of hook_menu().

File

./download_file.module, line 126
Module to direct download files or images.

Code

function download_file_direct_download($fid) {
  $fid = intval($fid);
  $file = file_load($fid);
  if (empty($fid) || empty($file) || !$file->status) {
    return drupal_access_denied();
  }
  $headers = array(
    'Content-Type' => 'force-download',
    'Content-Disposition' => 'attachment; filename="' . $file->filename . '"',
    'Content-Length' => $file->filesize,
    'Content-Transfer-Encoding' => 'binary',
    'Pragma' => 'no-cache',
    'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
    'Expires' => '0',
    'Accept-Ranges' => 'bytes',
  );

  // Let other modules provide headers and controls access to the file.
  if (in_array(-1, $headers)) {
    return drupal_access_denied();
  }
  if (count($headers)) {
    file_transfer($file->uri, $headers);
  }
}