Skip to content

Instantly share code, notes, and snippets.

@johnmcc
Forked from anonymous/download_file.module
Created December 14, 2012 12:18
Show Gist options
  • Save johnmcc/4285059 to your computer and use it in GitHub Desktop.
Save johnmcc/4285059 to your computer and use it in GitHub Desktop.
<?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