function download($front = false) { global $mainframe; jimport('joomla.filesystem.file'); $params = &JComponentHelper::getParams('com_k2'); $id = JRequest::getInt('id'); JPluginHelper::importPlugin('k2'); $dispatcher = &JDispatcher::getInstance(); $dispatcher->trigger('onK2Download'); $attachment = &JTable::getInstance('K2Attachment', 'Table'); $attachment->load($id); $path = $params->get('attachmentsFolder', NULL); if (is_null($path)) { $savepath = JPATH_ROOT.DS.'media'.DS.'k2'.DS.'attachments'; } else { $savepath = $path; } $file = $savepath.DS.$attachment->filename; if (JFile::exists($file)) { if ($front) $attachment->hit(); $len = filesize($file); $filename = basename($file); // // // // Let's check the file extension for right Content-Type // // $file_extension = strtolower(substr(strrchr($filename,"."),1)); //This will set the Content-Type to the appropriate setting for the file switch( $file_extension ) { case "pdf": $ctype="application/pdf"; break; case "exe": $ctype="application/octet-stream"; break; case "zip": $ctype="application/zip"; break; case "doc": $ctype="application/msword"; break; case "xls": $ctype="application/vnd.ms-excel"; break; case "ppt": $ctype="application/vnd.ms-powerpoint"; break; case "gif": $ctype="image/gif"; break; case "png": $ctype="image/png"; break; case "jpeg": case "jpg": $ctype="image/jpg"; break; case "mp3": $ctype="audio/mpeg"; break; case "wav": $ctype="audio/x-wav"; break; case "mpeg": case "mpg": case "mpe": $ctype="video/mpeg"; break; case "mov": $ctype="video/quicktime"; break; case "avi": $ctype="video/x-msvideo"; break; //The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files) default: $ctype="application/force-download"; } header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private", false); header("Content-Type: ".$ctype); header("Content-Disposition: attachment; filename=".$filename.";"); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".$len); readfile($file); } else { echo JText::_('File does not exist'); } $mainframe->close(); }