您的位置:首页 > 编程语言 > PHP开发

php下载远程大文件(获取远程文件大小)

2017-06-16 16:47 746 查看
function download_file($url)
{
// $url = http://192.168.8.95/vm/download_file?downurl=http://192.168.8.96/download/test1.tar.gz; // 要下载的文件可能在本地,也可能在其他节点服务器
if (empty($url) || false === @fopen($url, 'rb')) {
exit('文件不存在');
}

// 处理中文名
$filename = basename($url);
$user_agent  = $_SERVER["HTTP_USER_AGENT"];
if (false !== stripos($user_agent, 'Trident')) {
$filename = rawurlencode($filename);
}

// 文件目标
list($host, $dl_file) = explode('download', $url);
$server_addr = $_SERVER['SERVER_ADDR'];
if (false !== stripos($host, $server_addr)) {
// 本地文件
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('X-Accel-Redirect: /download' . $dl_file);
exit;
} else {
// 文件在其他节点服务器
$header = get_headers($url, 1);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Length: ' . $header['Content-Length']);
readfile($url);
exit;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: