PHP Send data

Source: Internet
Author: User
Tags sendfile
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 '];


$MD 5 = MD5 ($fileStat [' mtime ']. ' ='. $fileStat [' ino ']. ' ='. $fileStat [' size ']);
$etag = ' "'. $MD 5. '-' . CRC32 ($MD 5). '"';


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 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 ');
?>

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.