PHP download, support a variety of files and breakpoints continue to pass
function Download ($url, $filename) {
Get file size, prevent more than 2G of files, read with sprintf
$filesize = sprintf ("%u", FileSize ($url));
if (! $filesize) {
Return
}
Header ("content-type:application/octet-stream\n"); Application/octet-stream
Header ("Content-type:unknown/unknown;");
Header ("content-disposition:attachment; Filename=\ "". $filename. "\"" );
Header (' content-transfer-encoding:binary ');
if ($range = getenv (' Http_range ')) {//when there is an offset, use a 206 breakpoint to continue the header
$range = explode (' = ', $range);
$range = $range [1];
Header ("http/1.1 206 Partial Content");
Header ("Date:".) Gmdate ("D, D M Y h:i:s"). "GMT");
Header ("last-modified:".) Gmdate ("D, D M Y h:i:s", Filemtime ($url)). "GMT");
Header ("Accept-ranges:bytes");
Header ("Content-length:".) ($filesize-$range)); The
header ("Content-range:bytes"). $range. ($filesize-1). "/" . $filesize); The
header ("Connection:close"). "\ n");
} else {
header ("Content-length:". $filesize. "\ n");
$range = 0;
}
LoadFile ($url);
}
function LoadFile ($filename, $retbytes = True) {
$buffer = ";
$cnt = 0;
$handle = fopen ($filename, ' RB ');
if ($handle = = = False) {
return false;
}
while (! feof ($handle)) {
$buffer = Fread ($handle, 1024 * 1024);
Echo $buffer;
Ob_flush ();
Flush ();
if ($retbytes) {
$cnt + = strlen ($buffer);
}
}
$status = fclose ($handle);
if ($retbytes && $status) {
return $cnt;
}
return $status;
}
Download (".. /fujian/aa.pdf "," aa.pdf ");//Parameter one is the file path, the other is the file name
?>
PHP Download function code