Share a very useful phpheader download function. Copy the code as follows :? Php *** send file *** @ author: legend (legendsky@hotmail.com) * @ link: www.ugia.cn? P109 * @ description: sendfiletoclient * @ version: 1.0
The code is as follows:
/**
* Send a file
*
* @ Author: legend (legendsky@hotmail.com)
* @ Link: http://www.ugia.cn /? P = 109
* @ Description: send file to client
* @ Version: 1.0
*
* @ Param string $ fileName file name or path
* @ Param string $ fancyName: custom file name. if it is null, filename is used.
* @ Param boolean $ forceDownload whether to force download
* @ Param integer $ speedLimit speed limit, in bytes. 0 is unlimited and does not support windows servers.
* @ Param string $ contentType file type. the default value is 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).'" '); // automatically convert Chinese characters to URL encoding
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;
}
?>
The http://www.bkjia.com/PHPjc/728084.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/728084.htmlTechArticle code is as follows :? Php/*** send file *** @ author: legend (legendsky@hotmail.com) * @ link: http://www.ugia.cn /? P = 109 * @ description: send file to client * @ version: 1. 0...