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

PHP中文网
Lepaskan: 2023-03-10 18:16:02
asal
1117 orang telah melayarinya
<?<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>;
Salin selepas log masuk

 

Atas ialah kandungan terperinci php下载远程大文件(获取远程文件大小). Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan