分享一個超好用的php header下載函數(shù)
<?php
/**
* 發(fā)送文件
*
* @author: legend(legendsky@hotmail.com)
* @link: http://www.ugia.cn/?p=109
* @description: send file to client
* @version: 1.0
*
* @param string $fileName 文件名稱或路徑
* @param string $fancyName 自定義的文件名,為空則使用filename
* @param boolean $forceDownload 是否強制下載
* @param integer $speedLimit 速度限制,單位為字節(jié),0為不限制,不支持windows服務(wù)器
* @param string $$contentType 文件類型,默認(rèn)為application/octet-stream
*
* @return boolean
*/
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 == '')
{
$endPos = $fileSize - 1;
}
$startPos = $startPos < 0 ? 0 : $startPos;
$endPos = $endPos > $fileSize - 1 ? $fileSize - 1 : $endPos;
$length = $endPos - $startPos + 1;
if ($length < 0)
{
return false;
}
$contentLength = $length;
$isPartial = true;
}
}
// send headers
if ($isPartial)
{
header('HTTP/1.1 206 Partial Content');
header("Content-Range: bytes $startPos-$endPos/$fileSize");
}
else
{
header("HTTP/1.1 200 OK");
$startPos = 0;
$endPos = $contentLength - 1;
}
header('Pragma: cache');
header('Cache-Control: public, must-revalidate, max-age=0');
header('Accept-Ranges: bytes');
header('Content-type: ' . $contentType);
header('Content-Length: ' . $contentLength);
if ($forceDownload)
{
header('Content-Disposition: attachment; filename="' . rawurlencode($fancyName). '"');//漢字自動轉(zhuǎn)為URL編碼
header('Content-Disposition: attachment; filename="' . $fancyName. '"');
}
header("Content-Transfer-Encoding: binary");
$bufferSize = 2048;
if ($speedLimit != 0)
{
$packetTime = floor($bufferSize * 1000000 / $speedLimit);
}
$bytesSent = 0;
$fp = fopen($fileName, "rb");
fseek($fp, $startPos);
//fpassthru($fp);
while ($bytesSent < $contentLength && !feof($fp) && connection_status() == 0 )
{
if ($speedLimit != 0)
{
list($usec, $sec) = explode(" ", microtime());
$outputTimeStart = ((float)$usec + (float)$sec);
}
$readBufferSize = $contentLength - $bytesSent < $bufferSize ? $contentLength - $bytesSent : $bufferSize;
$buffer = fread($fp, $readBufferSize);
echo $buffer;
ob_flush();
flush();
$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);
}
}
}
return true;
}
?>
- php輸出xml必須header的解決方法
- PHP錯誤Warning: Cannot modify header information - headers already sent by解決方法
- PHP header()函數(shù)常用方法總結(jié)
- php用header函數(shù)實現(xiàn)301跳轉(zhuǎn)代碼實例
- Php header()函數(shù)語法及使用代碼
- php header功能的使用
- 分享PHP header函數(shù)使用教程
- 淺析php header 跳轉(zhuǎn)
- PHP 使用header函數(shù)設(shè)置HTTP頭的示例解析 表頭
- 探討php中header的用法詳解
- php 模擬get_headers函數(shù)的代碼示例
- PHP header()函數(shù)使用詳細(xì)(301、404等錯誤設(shè)置)
- PHP利用header跳轉(zhuǎn)失效的解決方法
相關(guān)文章
PHP實現(xiàn)動態(tài)添加XML中數(shù)據(jù)的方法
這篇文章主要介紹了PHP實現(xiàn)動態(tài)添加XML中數(shù)據(jù)的方法,結(jié)合實例形式分析了php操作xml格式數(shù)據(jù)類的定義及簡單使用技巧,需要的朋友可以參考下2018-03-03
php循環(huán)檢測目錄是否存在并創(chuàng)建(循環(huán)創(chuàng)建目錄)
php循環(huán)檢測目錄是否存在并創(chuàng)建,需要的朋友可以參考下。2011-01-01
PHP依賴注入(DI)和控制反轉(zhuǎn)(IoC)詳解
這篇文章主要介紹了PHP依賴注入(DI)和控制反轉(zhuǎn)(IoC)的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
php調(diào)用Workerman管理定時任務(wù)詳解
Workerman?是一個高性能的?PHP?Socket?框架,常用于開發(fā)實時通信、長連接服務(wù)等場景,本文主要來和大家聊聊如何使用Workerman管理定時任務(wù),感興趣的小伙伴可以參考一下2025-04-04
深入理解PHP中的static和yield關(guān)鍵字
這篇文章主要給大家介紹了關(guān)于PHP中static和yield關(guān)鍵字的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用php具有一定的參考學(xué)習(xí)價值,文章需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09

