Last active
September 22, 2018 02:37
-
-
Save sunghwan2789/0d6b5deee07b2d752294 to your computer and use it in GitHub Desktop.
이어받습니다
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 | |
function mimeTypeOf($path) | |
{ | |
$finfo = new finfo(FILEINFO_MIME_TYPE); | |
return $finfo->file($path); | |
} | |
function download($path, $name = '', $mimeType = '', $expires = 0, $streaming = false) | |
{ | |
// http://w-shadow.com/blog/2007/08/12/how-to-force-file-download-with-php/ | |
set_time_limit(0); | |
ini_set('zlib.output_compression', 'Off'); | |
$fp = @fopen($path, 'rb'); | |
if ($fp === false) | |
{ | |
echo '* File not found or inaccessible!'; | |
exit; | |
} | |
@ob_end_clean(); | |
if ($expires > 0) | |
{ | |
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT'); | |
header('Cache-Control: max-age=' . $expires); | |
} | |
else | |
{ | |
header('Pragma: no-cache'); | |
header('Expires: Thu, 01 Jan 1970 00:00:00 GMT'); | |
header('Cache-control: no-store'); | |
} | |
// https://tools.ietf.org/html/rfc6266 | |
$name = empty($name) ? basename($path) : $name; | |
header('Content-Type: ' . (empty($mimeType) ? mimeTypeOf($path) : $mimeType)); | |
header('Content-Disposition: ' . ($streaming ? 'inline' : 'attachment') . '; ' . | |
'filename="' . rawurlencode($name) . '"; ' . | |
'filename*=UTF-8\'\'' . rawurlencode($name)); | |
// https://tools.ietf.org/html/rfc7233 | |
$size = fstat($fp)['size']; | |
header('Accept-Ranges: bytes'); | |
if (isset($_SERVER['HTTP_RANGE'])) | |
{ | |
list($rangeUnit, $ranges) = explode('=', $_SERVER['HTTP_RANGE'], 2); | |
if ($rangeUnit == 'bytes') | |
{ | |
list($range, $extra_ranges) = explode(',', $ranges, 2); | |
} | |
else | |
{ | |
$range = ''; | |
} | |
list($start, $end) = explode('-', $range); | |
if (empty($start) && !empty($end)) | |
{ | |
$start = intval($end) ? max($size - intval($end), 0) : 0; | |
$end = $size - 1; | |
} | |
else | |
{ | |
$end = empty($end) ? $size - 1 : min(intval($end), $size - 1); | |
$start = empty($start) || intval($start) > $end ? 0 : intval($start); | |
} | |
// header('HTTP/1.1 206 Partial Content'); | |
http_response_code(206); | |
header('Content-Range: bytes ' . $start . '-' . $end . '/' . $size); | |
$size = $end - $start + 1; | |
fseek($fp, $start); | |
} | |
header('Content-Length: ' . $size); | |
flush(); | |
$send = 0; | |
while (!connection_aborted() && !feof($fp) && $send < $size) | |
{ | |
$buffer = fread($fp, min($size - $send, 1024 * 1024 * 4)); | |
echo $buffer; | |
flush(); | |
$send += strlen($buffer); | |
} | |
fclose($fp); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment