这适用于浏览器中的 pdf 渲染(例如:PDFJS)、mp4 视频流或下载文件。
$headers = new \Zend\Http\Headers();
$headers->addHeaderLine('Content-Type', $type)
->addHeaderLine('Cache-Control', 'must-revalidate')
->addHeaderLine('Pragma', 'public');
$response = new \Zend\Http\Response();
$downloadFile = false | true; // <-- Change this
if($downloadFile) {
$headers->addHeaderLine('Content-Disposition', 'attachment; filename="' . $fileName . '"')
->addHeaderLine('Content-Length', $size);
// ->addHeaderLine('Set-Cookie', 'fileDownload=true; path=/');
$response = new \Zend\Http\Response\Stream();
$response->setStream(fopen($pathFile, 'r'));
$response->setHeaders($headers);
return $response;
}
$size = filesize($pathFile);
$start = 0;
$end = $size - 1;
$buffer = 1024000;
// Get the 'Range' header if one was sent
if (isset($_SERVER['HTTP_RANGE'])) {
$range = $_SERVER['HTTP_RANGE']; // IIS/Some Apache versions
} else if ($apache = apache_request_headers()) { // Try Apache again
$arrHeaders = array();
foreach ($apache as $header => $val) {
$arrHeaders[strtolower($header)] = $val;
}
if (isset($arrHeaders['range'])) {
$range = $arrHeaders['range'];
} else {
$range = false; // We can't get the header/there isn't one set
}
} else {
$range = false; // We can't get the header/there isn't one set
}
// Get the data range requested (if any)
$size = filesize($pathFile);
if ($range) {
$partial = true;
list($param,$range) = explode('=',$range);
if (strtolower(trim($param)) != 'bytes') { // Bad request - range unit is not 'bytes'
$response->setStatusCode(400); // 400 Invalid Request
exit;
}
$range = explode(',',$range);
$range = explode('-',$range[0]); // We only deal with the first requested range
if (count($range) != 2) { // Bad request - 'bytes' parameter is not valid
$response->setStatusCode(400); // 400 Invalid Request
exit;
}
if ($range[0] === '') { // First number missing, return last $range[1] bytes
$end = $size - 1;
$start = $end - intval($range[1]);
} else if ($range[1] === '') { // Second number missing, return from byte $range[0] to end
$start = intval($range[0]);
$end = $size - 1;
} else { // Both numbers present, return specific range
$start = intval($range[0]);
$end = intval($range[1]);
if ($end >= $size || (!$start && (!$end || $end == ($size - 1)))) {
$partial = false; // Invalid range/whole file specified, return whole file
}
}
$length = $end - $start + 1;
} else {
$partial = false; // No range requested
$length = $size;
}
// error_log(var_export(array($range, $partial, $length), true));
// Send standard headers
$headers->addHeaderLine("Content-Length", $length);
$headers->addHeaderLine("Accept-Ranges", "bytes");
// if requested, send extra headers and part of file...
if ($partial) {
$response->setStatusCode(206); // 206 (Partial Content)
$headers->addHeaderLine("Content-Range", "bytes $start-$end/".$size);
if (!$fp = fopen($pathFile, 'r')) { // Error out if we can't read the file
$response->setStatusCode(500); // 500 Internal Server Error
exit;
}
if ($start) {
fseek($fp,$start);
}
$out = '';
while ($length) { // Read in blocks of 8KB so we don't chew up memory on the server
$read = ($length > 8192) ? 8192 : $length;
$length -= $read;
$out .= fread($fp,$read);
}
fclose($fp);
} else {
$out = readfile($pathFile); // ...otherwise just send the whole file
}
$response->setContent($out);
$response->setHeaders($headers);
return $response;