php下载远程大文件(获取远程文件大小)
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; } }