PHP oversized File Downloader [HTTP cache negotiation, ETag tagging, breakpoint continuation]_php Tutorial

Source: Internet
Author: User

002 $file _path = './download/download_cn.rar ';

003

004//How to use

005 Downfile ($file _path);

006

007//server file path, download file name (default is server file name), whether to license user download method (default), speed limit (default automatic), file type (default all)

008 function Downfile ($fileName, $fancyName = ', $forceDownload = true, $speedLimit = 0, $contentType = ') {

009 if (!is_readable ($fileName))

010 {

011 header ("http/1.1 404 Not Found");

012 return false;

013}

014

015 $fileStat = stat ($fileName);

016 $lastModified = $fileStat [' Mtime '];

017

018 $MD 5 = MD5 ($fileStat [' Mtime ']. ='. $fileStat [' ino ']. ' ='. $fileStat [' size ']);

019 $etag = ' "'. $MD 5. '-' . CRC32 ($MD 5). '"';

020

021 Header (' last-modified: '. Gmdate ("D, D M Y h:i:s", $lastModified). ' GMT ');

022 Header ("ETag: $etag");

023

024 if (isset ($_server[' http_if_modified_since ") && strtotime ($_server[' http_if_modified_since ']) >= $ LastModified)

025 {

026 header ("http/1.1 304 not Modified");

027 return true;

028}

12X

030 if (isset ($_server[' http_if_unmodified_since ') && strtotime ($_server[' http_if_unmodified_since ']) < $lastModified)

031 {

032 Header ("http/1.1 304 not Modified");

033 return true;

034}

035

036 if (isset ($_server[' Http_if_none_match ") && $_server[' http_if_none_match '] = = $etag)

037 {

038 Header ("http/1.1 304 not Modified");

039 return true;

040}

041

042 if ($fancyName = = ")

043 {

044 $fancyName = basename ($fileName);

045}

046

047 if ($contentType = = ")

048 {

049 $contentType = ' Application/octet-stream ';

050}

051

052 $fileSize = $fileStat [' Size '];

053

054 $contentLength = $fileSize;

055 $isPartial = false;

15W

057 if (isset ($_server[' Http_range '))

058 {

059 if (Preg_match ('/^bytes= (d*)-(d*) $/', $_server[' Http_range '], $matches))

060 {

061 $startPos = $matches [1];

062 $endPos = $matches [2];

063

064 if ($startPos = = "&& $endPos = =")

065 {

066 return false;

067}

068

069 if ($startPos = = ")

070 {

071 $startPos = $fileSize-$endPos;

072 $endPos = $fileSize-1;

073}

074 else if ($endPos = = ")

075 {

076 $endPos = $fileSize-1;

077}

078

079 $startPos = $startPos < 0? 0: $startPos;

080 $endPos = $endPos > $fileSize-1? $fileSize-1: $endPos;

081

082 $length = $endPos-$startPos + 1;

083

084 if ($length < 0)

085 {

086 return false;

087}

088

089 $contentLength = $length;

090 $isPartial = true;

091}

092}

093

094//Send headers

095 if ($isPartial)

096 {

097 header (' http/1.1 206 Partial Content ');

098 header ("Content-range:bytes $startPos-$endPos/$fileSize");

099

100}

101 Else

102 {

103 header ("http/1.1 OK");

104 $startPos = 0;

$endPos = $contentLength-1;

106}

107

108 header (' Pragma:cache ');

109 Header (' Cache-control:public, must-revalidate, max-age=0 ');

The header (' accept-ranges:bytes ');

111 Header (' Content-type: '. $contentType);

The header (' Content-length: '. $contentLength);

113

if ($forceDownload)

115 {

The header (' content-disposition:attachment; Filename= '. Rawurlencode ($fancyName). '"');

117}

118

119 Header ("Content-transfer-encoding:binary");

120

121 $bufferSize = 2048;

122

123 if ($speedLimit! = 0)

124 {

$packetTime = Floor ($bufferSize * 1000000/$speedLimit);

126}

127

$bytesSent = 0;

129 $fp = fopen ($fileName, "RB");

Fseek ($fp, $startPos);

131 while ($bytesSent < $contentLength &&!feof ($fp) && connection_status () = = 0)

132 {

133 if ($speedLimit! = 0)

134 {

135 list ($usec, $sec) = Explode ("", Microtime ());

136 $outputTimeStart = ((float) $usec + (float) $sec);

137}

138

139 $readBufferSize = $contentLength-$bytesSent < $bufferSize? $contentLength-$bytesSent: $bufferSize;

$buffer = Fread ($fp, $readBufferSize);

141

142 echo $buffer;

143

144 Ob_flush ();

145 flush ();

146

147 $bytesSent + = $readBufferSize;

148

149 if ($speedLimit! = 0)

150 {

151 list ($usec, $sec) = Explode ("", Microtime ());

$outputTimeEnd = ((float) $usec + (float) $sec);

153

154 $useTime = ((float) $outputTimeEnd-(float) $outputTimeStart) * 1000000;

155 $sleepTime = Round ($packetTime-$useTime);

156 if ($sleepTime > 0)

157 {

158 usleep ($sleepTime);

159}

160}

161}

162 return true;

163}

164?>


http://www.bkjia.com/PHPjc/444967.html www.bkjia.com true http://www.bkjia.com/PHPjc/444967.html techarticle PHP Tutorial 002 $file _path = './download/download_cn.rar '; 003 004//Method of Use 005 downfile ($file _path); 006 007//server file path, download file First name (default is server text ...)

  • 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.