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

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

PHP中文网
发布: 2023-03-10 18:16:02
原创
1138 人浏览过
<?<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中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板