亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

強制文件下載處理

強制文件下載處理

PHP
搖曳的薔薇 2023-04-02 10:19:07
我試圖了解如何為強制下載創建響應以及瀏覽器如何處理它。在此處關注本文:教程。我有一個發送文件作為下載響應的腳本。<?php// it's a zip fileheader('Content-Type: application/zip');// 1 million bytes (about 1megabyte)header('Content-Length: 1000000');// load a download dialogue, and save it as download.zipheader('Content-Disposition: attachment; filename="download.zip"');// 1000 times 1000 bytes of datafor ($i = 0; $i < 1000; $i++) {    echo str_repeat(".",1000);    // sleep to slow down the download    // sleep(5);}sleep(5);當sleep()函數在循環內時,它會在文件開始下載之前等待一段時間。但是當放在循環之外時,文件會立即開始下載。誰能幫我理解這種行為?
查看完整描述

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;


查看完整回答
反對 回復 2023-04-02
  • 1 回答
  • 0 關注
  • 115 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號