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

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

Release time:2018-10-19 11:14:00 Author:Aoe Wide Reading volume:244
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;
    }
}
  
I want to comment
Leave a message
http://hljmdd.cn/index.php/
User login
You have not written any reviews yet!
You have commented!
Can only praise once!
You have a collection!