You are here

file_force.module in File Force Download 5

Same filename and directory in other branches
  1. 6.2 file_force.module
  2. 6 file_force.module
  3. 7 file_force.module

File

file_force.module
View source
<?php

/**
 * File Force module for Drupal
 * by Garrett Albright
 *
 * Cheap caffeine goes in. Quality code comes out.
 */

/**
 * Implementation of hook_menu().
 * Note that the D5 and D6 versions of this module are identical in all ways,
 * which is why the function below contains elements for both versions.
 */
function file_force_menu() {
  return array(
    'files/download' => array(
      // Drupal 6
      'path' => 'files/download',
      // Drupal 5
      'title' => 'File Force',
      'description' => 'Force visitors to download requested files',
      'callback' => 'file_force_go',
      // Drupal 5
      'page callback' => 'file_force_go',
      // Drupal 6
      'access' => array(
        'access content',
      ),
      // Drupal 5
      'access arguments' => array(
        'access content',
      ),
      // Drupal 6
      'type' => MENU_CALLBACK,
    ),
  );
}

/**
 * Implementation of hook_file_download().
 * This is what adds the headers which activates the force downloading.
 */
function file_force_file_download($filepath) {
  if (!defined('FILE_FORCE_ACTIVE')) {

    // Our menu hook wasn't called, so we should ignore this.
    return NULL;
  }
  return array(
    'Content-Type: application/octet-stream',
    'Content-Disposition: attachment; filename="' . basename($filepath) . '";',
    // Content-Length is also a good header to send, as it allows the browser to
    // display a progress bar correctly. It looks like on D5, the Upload module
    // does this too if it's installed, but not on D6. So let's do it ourselves
    // just in case.
    // There's a trick for determining the file size for files over 2 GB. Nobody
    // should be using this module with files that large, but… the sprintf()
    // trickery makes sure the value is correct for files larger than 2GB. See
    // note at http://php.net/filesize
    'Content-Length: ' . sprintf('%u', filesize($filepath)),
  );
}

/**
 * The function that does stuff. This makes the file path correspond to the
 * correct filesystem path, lets us know that we should respond when our
 * hook_file_download() implementation is called, and finally calls
 * file_download() to start the download process.
 */
function file_force_go() {
  $args = func_get_args();
  $filepath = file_directory_path() . '/' . implode('/', $args);
  define('FILE_FORCE_ACTIVE', TRUE);
  file_download($filepath);
}

Functions

Namesort descending Description
file_force_file_download Implementation of hook_file_download(). This is what adds the headers which activates the force downloading.
file_force_go The function that does stuff. This makes the file path correspond to the correct filesystem path, lets us know that we should respond when our hook_file_download() implementation is called, and finally calls file_download() to start the download process.
file_force_menu Implementation of hook_menu(). Note that the D5 and D6 versions of this module are identical in all ways, which is why the function below contains elements for both versions.