首页  >  文章  >  后端开发  >  php下载远程大文件(获取远程文件大小)

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

PHP中文网
PHP中文网原创
2017-06-17 16:00:541112浏览
<?<span style="color: #000000">php
    </span><span style="color: #008000">//</span><span style="color: #008000"> 暂不支持断点续传
    // $url = 'http://www.mytest.com/debian.iso'; 不知道为何获取本地文件大小为0</span>
    <span style="color: #800080">$url</span> = 'http://192.168.8.93/download/vm-672/18/0.vmdk'<span style="color: #000000">;
    </span><span style="color: #800080">$file</span> = <span style="color: #008080">basename</span>(<span style="color: #800080">$url</span><span style="color: #000000">);
    </span><span style="color: #800080">$header</span> = <span style="color: #008080">get_headers</span>(<span style="color: #800080">$url</span>, 1<span style="color: #000000">);
    </span><span style="color: #800080">$size</span> = <span style="color: #800080">$header</span>['Content-Length'<span style="color: #000000">];

    </span><span style="color: #800080">$fp</span> = <span style="color: #008080">fopen</span>(<span style="color: #800080">$url</span>, 'rb'<span style="color: #000000">);
    </span><span style="color: #0000ff">if</span> (<span style="color: #800080">$fp</span> === <span style="color: #0000ff">false</span>) <span style="color: #0000ff">exit</span>('文件不存在或打开失败'<span style="color: #000000">);

    </span><span style="color: #008080">header</span>('Content-Description: File Transfer'<span style="color: #000000">);
    </span><span style="color: #008080">header</span>('Content-Type: application/octet-stream'<span style="color: #000000">);
    </span><span style="color: #008080">header</span>('Content-Disposition: attachment; filename="'.<span style="color: #800080">$file</span>.'"'<span style="color: #000000">);
    </span><span style="color: #008080">header</span>('Content-Transfer-Encoding: binary'<span style="color: #000000">);
    </span><span style="color: #008080">header</span>('Expires: 0'<span style="color: #000000">);
    </span><span style="color: #008080">header</span>('Cache-Control: must-revalidate, post-check=0, pre-check=0'<span style="color: #000000">);
    </span><span style="color: #008080">header</span>('Pragma: public'<span style="color: #000000">);
    </span><span style="color: #008080">header</span>('Content-Length: ' . <span style="color: #800080">$size</span><span style="color: #000000">);

    </span><span style="color: #008080">ob_clean</span><span style="color: #000000">();
    </span><span style="color: #008080">ob_end_flush</span><span style="color: #000000">();
    </span><span style="color: #008080">set_time_limit</span>(0<span style="color: #000000">);
    
    </span><span style="color: #800080">$chunkSize</span> = 1024 * 1024<span style="color: #000000">;
    </span><span style="color: #0000ff">while</span> (!<span style="color: #008080">feof</span>(<span style="color: #800080">$fp</span><span style="color: #000000">)) {
        </span><span style="color: #800080">$buffer</span> = <span style="color: #008080">fread</span>(<span style="color: #800080">$fp</span>, <span style="color: #800080">$chunkSize</span><span style="color: #000000">);
        </span><span style="color: #0000ff">echo</span> <span style="color: #800080">$buffer</span><span style="color: #000000">;
        </span><span style="color: #008080">ob_flush</span><span style="color: #000000">();
        </span><span style="color: #008080">flush</span><span style="color: #000000">();
    }
    </span><span style="color: #008080">fclose</span>(<span style="color: #800080">$fp</span><span style="color: #000000">);
    </span><span style="color: #0000ff">exit</span>;

 

以上是php下载远程大文件(获取远程文件大小)的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn