PHP function code for downloading files via functions
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));
Header ("Content-range:bytes". $range. ($filesize-1). "/" . $filesize);
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 num. Bytes delivered like ReadFile () does.
}
return $status;
}
Enter 2 parameters to complete the download
Download ($url, $filename)
The above describes the PHP download file functions, including the content of PHP download, I hope to be interested in PHP tutorial friends helpful.