Created
December 14, 2012 12:17
-
-
Save anonymous/4285054 to your computer and use it in GitHub Desktop.
A quick Drupal module which allows you to force download files (if you can't set headers through .htaccess, for example)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Link to /download_file/$fid to force download rather than view in browser | |
/* | |
* Implements hook_menu() | |
*/ | |
function download_file_menu(){ | |
$items = array(); | |
$items['download_file/%'] = array( | |
'title' => 'Download files', | |
'description' => 'Description', | |
'page callback' => 'download_file_view', | |
'page arguments' => array(1), | |
'access callback' => TRUE, // or 'check_access' | |
'type' => MENU_CALLBACK | |
); | |
return $items; | |
} | |
function download_file_view($fid){ | |
$file = file_load($fid); | |
if(isset($file->uri)){ | |
$url = file_create_url($file->uri); | |
header('Content-Type: application/octet-stream'); | |
header("Content-Transfer-Encoding: Binary"); | |
header("Content-disposition: attachment; filename=\"".$file->filename."\""); | |
readfile($url); | |
}else{ | |
drupal_not_found(); | |
} | |
exit; | |
} | |
function check_access(){ | |
global $user; | |
return $user->uid > 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment