function webfm_send_file in Web File Manager 5.2
Same name and namespace in other branches
- 5 webfm.module \webfm_send_file()
webfm_send_file - streams a file privately for download
If $fid arg is numeric then file path is referenced via db, otherwise the arg .. is a base 64 encoded path that must be concatenated to base file dir
Parameters
object $fid - file id:
bool $attach - 1 = attach / 0 = inline:
1 string reference to 'webfm_send_file'
- webfm_menu in ./
webfm.module - Implementation of hook_menu().
File
- ./
webfm.module, line 1751
Code
function webfm_send_file($fid, $attach = false) {
if (is_numeric($fid)) {
if (($f = webfm_get_file_record($fid)) === FALSE) {
print theme('page', "");
return;
}
}
else {
$f = new stdClass();
$f->fpath = file_directory_path() . base64_decode($fid);
if (!is_file($f->fpath)) {
print theme('page', "");
return;
}
}
$name = basename($f->fpath);
//filenames in IE containing dots will screw up the
//filename unless we add this
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
$name = preg_replace('/\\./', '%2e', $name, substr_count($name, '.') - 1);
}
// Get file extension
$ext = explode('.', $name);
$extension = $ext[count($ext) - 1];
// Try and find appropriate type
switch (strtolower($extension)) {
case 'txt':
$type = 'text/plain';
break;
case "pdf":
$type = 'application/pdf';
break;
case "exe":
$type = 'application/octet-stream';
break;
case "zip":
$type = 'application/zip';
break;
case "doc":
$type = 'application/msword';
break;
case "xls":
$type = 'application/vnd.ms-excel';
break;
case "ppt":
$type = 'application/vnd.ms-powerpoint';
break;
case "gif":
$type = 'image/gif';
break;
case "png":
$type = 'image/png';
break;
case "jpg":
$type = 'image/jpeg';
break;
case "jpeg":
$type = 'image/jpeg';
break;
case "html":
$type = 'text/html';
break;
default:
$type = 'application/force-download';
}
//download headers:
$header = array();
if ($attach === '1') {
// prompt for download file or view
$header[] = 'Pragma: no-cache';
$header[] = 'Cache-Control: no-cache, must-revalidate';
$header[] = 'Content-Disposition: attachment; filename="' . $name . '";';
}
else {
// view file via browser
$header[] = 'Pragma: public';
// required
$header[] = 'Expires: 0';
$header[] = 'Cache-Control: must-revalidate, post-check=0, pre-check=0';
$header[] = 'Content-Transfer-Encoding: binary';
$header[] = 'Content-Disposition: inline; filename="' . $name . '";';
}
$header[] = 'Content-Type: ' . $type;
$header[] = 'Content-Length: ' . (string) filesize($f->fpath);
$header[] = 'Connection: close';
//drupal file_transfer will fail if file is not inside file system directory
file_transfer($f->fpath, $header);
}