Php sends a file & lt ;? PhpfunctionsendFile ($ fileName, $ fancyName, $ forceDownloadtrue, $ speedLimit0, $ contentType) {& nbsp; if (! Is_readable ($ fileName) & nbsp; & php send file
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 ).'"');
}
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;
}
SendFile ('../document.zip', 'document.zip ');
?>