通过函数完成下载文件的PHP功能代码
function download($url, $filename) {
// 获得文件大小, 防止超过2G的文件, 用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' )) { // 当有偏移量的时候,采用206的断点续传头
$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: &