-
-
/**
- * 傳送檔案
- *
- * @param string $fileName 檔案名稱或路徑
- * @param string $fancyName 自訂的檔案名稱,為空白則使用filename
- * @param booleanName 自訂的檔案名稱,為空則使用filename
- * @param booleanName 自訂的檔案名稱,為空則使用filename
- * @param booleanName $forceDownload 是否強制下載
- * @param integer $speedLimit 速度限制,單位為位元組,0為不限制,不支援windows伺服器
- * @param string $$contentType 檔案類型,預設為application/octet- stream
- *
- * @return boolean
- */
- function sendFile($fileName, $fancyName = ' ', $forceDownload = true, $speedLimit = 0, $contentType = '')
- {
- if (!is_read($fileName))
- {
- header("HTTP/1.1 404 找不到") ;
- return false;
- }
- $fileStat = stat($fileName);
- $lastModified = $fileStat['mtime'];
; $md5 = md5($fileStat['mtime'] .'='.$fileStat['ino'] .'='.$fileStat['size']);
- $etag = '"' . $md5 '-' .crc32($md5) '"';
- header('最後修改時間: ' .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']) >= $ > {
- header("HTTP/1.1 304 未修改");
- return true;
- }
- if (isset($_SERVER['HTTP_IF_UNMODIFIED_SINCE']) & strtotime(P.D. ) ']) {
- header("HTTP /1.1 304 未");
- return true;
- }
- if (isset($_SERVER['HTTP_IF_NONE_MATMATCH'] ) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag)
- {
- header("HTTP/1.1 304 未修改");
- return true;
- }
- }
- if ($ffancyName == '')
- {
- $fancyName = 基本名稱($檔名);
- }
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 == '')
- {
- $endPos = $fileSize - 1;
- }
- $startPos = $startPos $endPos = $endPos > $檔案大小 - 1 ? $fileSize - 1 : $endPos;
- $length = $endPos - $startPos 1;
- if ($length {
- return false;
- }
- {
- return false;
- }
- $ contentLength = $length;
- $isPartial = true;
- }
- }
// 傳送headers
- if ($isPartial)
- {
- header ('HTTP/1.1 206 部分內容');
- header("Content-Range: bytes $startPos-$endPos/$fileSize ");
}
- else
- {
- header("HTTP/1.1 200 OK");
- $startPos = 0;
- $endPos = $contentLength - 1;
- }
- header ('Pragma: 儲存') ;
- header('Cache-Control: public, Must-revalidate, max-age=0');
- header('Accept-Ranges: bytes');
- header ('內容類型:' . $contentType);
- header('內容長度:' . $contentLength);
if ($forceDownload)
- {
- header('Content-Disposition:attachment; filename="' .rawurlencode($fancyName). '"');//漢字自動轉為編碼URL
- header( 'Content-Disposition:attachment; filename="' . $fancyName.'"');
- }
- header("Content-Transfer-Encoding:binary");
- // header 函數實作檔案下載
$bufferSize = 2048; if ($ speedLimit != 0) { $packetTime = Floor($bufferSize * 1000000 / $speedLimit); }
- $bytesSent = 0;
- $fp = fopen($fileName, "rb");
- fseek($fp, $startPos);
- //fpassthru($fp); p >
while ($bytesSent {
- if ($speedLimit != 0)
- {
- if ($speedLimit != 0)
- {
- { list($usec, $sec) = 爆炸(" ", microtime());
- $outputTimeStart = ((float)$usec (float)$sec);
- } // bbs.it-home.org
- $readBufferSize = $contentLength - $bytesSent
- $ 寬度尺寸$ contentLength - $bytesSent : $bufferSize;
- $buffer = fread($fp, $readBufferSize);
- echo $buffer;
- ob_flush();
- lush();
- $bytesSent = $readBufferSize;
if ($speedLimit != 0)
- {
- list($usec, $sec) = Explode(" ", microtime());
- $outputTimeEnd = ((float)$usec (float)$sec);
$useTime = ((float) $outputTimeEnd - (float) $outputTimeStart) * 1000000;
- $sleepTime = round($packetTime - $useTime);
- if ($sleepTime > 0)
- {
- usleep($sleepTime);
- }
- }
- }
- }
回傳true; } ?>
|
複製程式碼