Skip to content

Instantly share code, notes, and snippets.

@jlainezs
Created March 19, 2013 08:15
Show Gist options
  • Save jlainezs/5194449 to your computer and use it in GitHub Desktop.
Save jlainezs/5194449 to your computer and use it in GitHub Desktop.
File downloader to use under Joomla.
<?php
/**
* File downloader
* @author Pep Lainez
*
*/
defined('_JEXEC') or die('Restricted access');
jimport( 'joomla.filesystem.file' );
/**
*
* @author Pep Lainez
* @see http://blog.unijimpe.net/forzar-descarga-con-php/
*/
class DownloadFile{
// @todo possible security hole : docPath points to ANY file
static function getFile($docPath){
$user = JFactory::getUser();
if (!$user->guest){
if (is_file($docPath)){
$size = filesize($docPath);
if (function_exists('mime_content_type'))
$type=mime_content_type($path);
else {
if (function_exists("finfo_file")){
$info = finfo_open(FILEINFO_MIME);
$type = finfo_file($info, $path);
finfo_close($info);
}
}
if ($type == '')
$type = "application/force-download";
$filename = JFile::getName($docPath);
header('Content-Type: '.$type);
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$size);
readfile($path);
}
die();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment