zoukankan      html  css  js  c++  java
  • 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; } }
  • 相关阅读:
    软件工程 团队开发(2)
    软件工程 团队开发(1)
    大道至简阅读笔记01
    小工具集合用户模板和用户场景
    人月神话阅读笔记03
    人月神话阅读笔记02
    本周java学习
    本周学习总结
    本周java 学习进度报告
    《构建之法》读后感
  • 原文地址:https://www.cnblogs.com/gentsir/p/7028253.html
Copyright © 2011-2022 走看看