2016-09-24 14:32:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Vn\Web;
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
class Util {
|
2016-09-24 14:32:31 +00:00
|
|
|
/**
|
|
|
|
* Reads a file and writes it to the output buffer.
|
|
|
|
*
|
|
|
|
* @param string file The file path
|
|
|
|
* @param boolean useXsendfile Wether to use the apache module Xsendfile
|
2017-11-29 10:01:48 +00:00
|
|
|
*/
|
2018-05-23 10:14:20 +00:00
|
|
|
static function printFile($file, $useXsendfile = FALSE) {
|
|
|
|
if (!file_exists($file)) {
|
|
|
|
http_response_code(404);
|
2016-09-24 14:32:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
$finfo = new \finfo(FILEINFO_MIME_TYPE);
|
|
|
|
$mimeType = $finfo->file($file);
|
2016-09-24 14:32:31 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if ($useXsendfile) {
|
|
|
|
header("X-Sendfile: $file");
|
|
|
|
header("Content-Type: $mimeType");
|
2018-05-23 11:09:55 +00:00
|
|
|
} else {
|
2018-05-23 10:14:20 +00:00
|
|
|
header('Content-Description: File Transfer');
|
|
|
|
header("Content-Type: $mimeType");
|
|
|
|
header('Content-Disposition: attachment; filename="'. basename($file) .'"');
|
|
|
|
header('Expires: 0');
|
|
|
|
header('Cache-Control: must-revalidate');
|
|
|
|
header('Pragma: public');
|
|
|
|
header('Content-Length: '. filesize($file));
|
2016-09-24 14:32:31 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
set_time_limit(0);
|
|
|
|
readfile($file);
|
2016-09-24 14:32:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|