/[drupal]/contributions/modules/file_force/file_force.module
ViewVC logotype

Contents of /contributions/modules/file_force/file_force.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Sun Jul 13 05:23:55 2008 UTC (16 months, 2 weeks ago) by garrettalbright
Branch: MAIN
CVS Tags: DRUPAL-5--1-1-BETA2, DRUPAL-6--1-1-BETA2, HEAD
Branch point for: DRUPAL-5, DRUPAL-6--1
Changes since 1.1: +33 -48 lines
File MIME type: text/x-php
More correct operation. Other modules' implementations of hook_file_download() are repsected.
1 <?php
2 // $Id$
3
4 /**
5 * File Force module for Drupal
6 * by Garrett Albright
7 *
8 * Cheap caffeine goes in. Quality code comes out.
9 */
10
11 /**
12 * Implementation of hook_menu().
13 * Note that the D5 and D6 versions of this module are identical in all ways,
14 * which is why the function below contains elements for both versions.
15 */
16
17 function file_force_menu() {
18 return array(
19 'files/download' => array( // Drupal 6
20 'path' => 'files/download', // Drupal 5
21 'title' => 'File Force',
22 'description' => 'Force visitors to download requested files',
23 'callback' => 'file_force_go', // Drupal 5
24 'page callback' => 'file_force_go', // Drupal 6
25 'access' => array('access content'), // Drupal 5
26 'access arguments' => array('access content'), // Drupal 6
27 'type' => MENU_CALLBACK,
28 ),
29 );
30 }
31
32 /**
33 * Implementation of hook_file_download().
34 * This is what adds the headers which activates the force downloading.
35 */
36
37 function file_force_file_download($filepath) {
38 if (!defined('FILE_FORCE_ACTIVE')) {
39 // Our menu hook wasn't called, so we should ignore this.
40 return NULL;
41 }
42 return array(
43 'Content-Type: application/octet-stream',
44 'Content-Disposition: attachment; filename="' . basename($filepath) . '";',
45 // Content-Length is also a good header to send, as it allows the browser to
46 // display a progress bar correctly. It looks like on D5, the Upload module
47 // does this too if it's installed, but not on D6. So let's do it ourselves
48 // just in case.
49 // There's a trick for determining the file size for files over 2 GB. Nobody
50 // should be using this module with files that large, but… the sprintf()
51 // trickery makes sure the value is correct for files larger than 2GB. See
52 // note at http://php.net/filesize
53 'Content-Length: ' . sprintf('%u', filesize($filepath)),
54 );
55 }
56
57 /**
58 * The function that does stuff. This makes the file path correspond to the
59 * correct filesystem path, lets us know that we should respond when our
60 * hook_file_download() implementation is called, and finally calls
61 * file_download() to start the download process.
62 */
63
64 function file_force_go() {
65 $args = func_get_args();
66 $filepath = file_directory_path() . '/' . implode('/', $args);
67 define('FILE_FORCE_ACTIVE', TRUE);
68 file_download($filepath);
69 }

  ViewVC Help
Powered by ViewVC 1.1.2