Maison  >  Article  >  développement back-end  >  PHP télécharge des fichiers volumineux distants (obtient la taille du fichier distant)

PHP télécharge des fichiers volumineux distants (obtient la taille du fichier distant)

PHP中文网
PHP中文网original
2017-06-17 16:00:541112parcourir
<?<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>;

 

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn