Home  >  Article  >  Backend Development  >  PHP downloads remote large files (gets remote file size)

PHP downloads remote large files (gets remote file size)

PHP中文网
PHP中文网Original
2017-06-17 16:00:541100browse
<?<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>;

 

The above is the detailed content of PHP downloads remote large files (gets remote file size). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn