php发送文件
<?php
function sendFile($fileName, $fancyName = '', $forceDownload = true, $speedLimit = 0, $contentType = '')
{
if (!is_readable($fileName))
{
header("HTTP/1.1 404 Not Found");
return false;
}
$fileStat = stat($fileName);
$lastModified = $fileStat['mtime'];
$md5 = md5($fileStat['mtime'] .'='. $fileStat['ino'] .'='. $fileStat['size']);
$etag = '"' . $md5 . '-' . crc32($md5) . '"';
header('Last-Modified: ' . gmdate("D, d M Y H:i:s", $lastModified) . ' GMT');
header("ETag: $etag");
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $lastModified)
{
header("HTTP/1.1 304 Not Modified");
return true;
}
if (isset($_SERVER['HTTP_IF_UNMODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_UNMODIFIED_SINCE']) < $lastModified)
{
header("HTTP/1.1 304 Not Modified");
return true;
}
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag)
{
header("HTTP/1.1 304 Not Modified");
return true;
}
if ($fancyName == '')
{
$fancyName = basename($fileName);
}
if ($contentType == '')
{
$contentType = 'application/octet-stream';
}
$fileSize = $fileStat['size'];
$contentLength = $fileSize;
$isPartial = false;
if (isset($_SERVER['HTTP_RANGE']))
{
if (preg_match('/^bytes=(\d*)-(\d*)$/', $_SERVER['HTTP_RANGE'], $matches))
{
$startPos = $matches[1];
$endPos = $matches[2];
if ($startPos == '' && $endPos == '')
{
return false;
}
if ($startPos == '')
{
$startPos = $fileSize - $endPos;
$endPos = $fileSize - 1;
}
else if ($endPos == '')