Created
March 19, 2013 08:15
-
-
Save jlainezs/5194449 to your computer and use it in GitHub Desktop.
File downloader to use under Joomla.
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 | |
/** | |
* 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