2017-01-12 20:45:01 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class FileDownloader extends FileManager {
|
|
|
|
|
|
|
|
private static $instance = null;
|
|
|
|
|
|
|
|
public static function getInstance() {
|
|
|
|
if (self::$instance === null) {
|
|
|
|
self::$instance = new FileDownloader();
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function __construct() {}
|
|
|
|
|
|
|
|
public function download() {
|
|
|
|
$fullFilePath = $this->getFullFilePath();
|
|
|
|
|
|
|
|
if(file_exists($fullFilePath) && is_file($fullFilePath)) {
|
|
|
|
header('Cache-control: private');
|
2018-05-03 02:40:44 +02:00
|
|
|
header('Content-Type: ' . $this->getFileContentType());
|
|
|
|
header('Content-Length: ' . filesize($fullFilePath));
|
|
|
|
header('Content-Disposition: filename=' . $this->getFileName());
|
2017-01-12 20:45:01 +01:00
|
|
|
|
|
|
|
flush();
|
|
|
|
$file = fopen($fullFilePath, 'r');
|
|
|
|
print fread($file, filesize($fullFilePath));
|
|
|
|
fclose($file);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function eraseFile() {
|
|
|
|
unlink($this->getLocalPath() . $this->getFileName());
|
|
|
|
}
|
2018-05-03 02:40:44 +02:00
|
|
|
|
|
|
|
public function getFileContentType() {
|
|
|
|
$fileExtension = substr($this->getFileName(), -3);
|
|
|
|
$contentTypes = [
|
|
|
|
'jpg' => 'image/jpeg',
|
|
|
|
'gif' => 'image/fig',
|
|
|
|
'png' => 'image/png',
|
|
|
|
'default' => 'application/octet-stream',
|
|
|
|
];
|
|
|
|
|
|
|
|
return $contentTypes[
|
|
|
|
array_key_exists($fileExtension, $contentTypes) ? $fileExtension : 'default'
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|