= $lastmodified)
025 {
026 header(http/1.1 304 not modified);
027 return true;
028 }
029
030 if (isset($_server['http_if_unmodified_since']) && strtotime($_server['http_if_unmodified_since'])
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;
056
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
080 $endpos = $endpos > $filesize - 1 ? $filesize - 1 : $endpos;
081
082 $length = $endpos - $startpos + 1;
083
084 if ($length
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 200 ok);
104 $startpos = 0;
105 $endpos = $contentlength - 1;
106 }
107
108 header('pragma: cache');
109 header('cache-control: public, must-revalidate, max-age=0');
110 header('accept-ranges: bytes');
111 header('content-type: ' . $contenttype);
112 header('content-length: ' . $contentlength);
113
114 if ($forcedownload)
115 {
116 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 {
125 $packettime = floor($buffersize * 1000000 / $speedlimit);
126 }
127
128 $bytessent = 0;
129 $fp = fopen($filename, rb);
130 fseek($fp, $startpos);
131 while ($bytessent
132 {
133 if ($speedlimit != 0)
134 {
135 list($usec, $sec) = explode( , microtime());
136 $outputtimestart = ((float)$usec + (float)$sec);
137 }
138
139 $readbuffersize = $contentlength - $bytessent
140 $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());
152 $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.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/444967.htmltecharticle?php教程 002 $file_path = './download/download_cn.rar'; 003 004 //使用方法 005 downfile($file_path); 006 007 // 服务器文件路径,下载文件名字(默认为服务器文...