note: optinal arg $download, is to read the file without serving it.
Comments to be added when I can be bothered to
The method downloadFile is of course the cause of concern.
Code: Select all
<?php
class jmt_Download
{
private $view;
private $size;
private $path;
private $type;
private $name;
public function __construct ($view, $file, $folder, $download = null)
{
if (!file_exists($fullpath = realpath($folder . '/' . $file))) {
throw new FileNotFoundException ('File path given does not exist.');
return;
}
$this->view = $view;
$this->path = $fullpath;
$this->name = $file;
$this->readProperties();
if (is_null($download)) {
$this->downloadFile();
} else {
$this->view->setTemplate('fileProperties.tpl');
$this->view->assignVariable('fileSize', $this->size);
}
}
private function readProperties ()
{
if (function_exists('mime_content_type')) {
$this->type = mime_content_type($this->path);
$this->view->assignVariable('mimetype', $this->type);
}
$this->size = filesize($this->path);
}
private function downloadFile ()
{
header('Content-disposition: attachment; filename=' . $this->name . ';');
header('Content-Length: ' . $this->size);
readfile($this->path);
}
public function getSize ()
{
return $this->size;
}
public function getType ()
{
return $this->type;
}
}
?>