1 回答

TA貢獻1799條經驗 獲得超9個贊
第二種情況的問題是在調用睡眠函數之前將文件發送給客戶端。您可以將輸出存儲在內部緩沖區中,并在睡眠功能之后發送。(我不建議將其用于生產。)試試這個修改后的程序:
<?php
// it's a zip file
header('Content-Type: application/zip');
// 1 million bytes (about 1megabyte)
header('Content-Length: 1000000');
// load a download dialogue, and save it as download.zip
header('Content-Disposition: attachment; filename="download.zip"');
//Turn on output buffering
ob_start();
// 1000 times 1000 bytes of data
for ($i = 0; $i < 1000; $i++) {
echo str_repeat(".",1000);
// sleep to slow down the download
// sleep(5);
}
//Store the contents of the output buffer
$buffer = ob_get_contents();
// Clean the output buffer and turn off output buffering
ob_end_clean();
sleep(5);
echo $buffer;
- 1 回答
- 0 關注
- 115 瀏覽
添加回答
舉報