dongxuan2015 2014-12-12 22:45
浏览 17
已采纳

如何在PHP中实时创建文件[关闭]

Is there a PHP script where I can download a file while I am creating it in PHP?

The problem is that I have a huge script in PHP that creates a file, and to generate it, it lasts about 5 minutes. After it generates it, it downloads it, here takes another 5 mins or so.

In total it takes more than 10 minutes to download a file.

What I need is to create a script somewhat like phpmyadmin where the file downloads while it is being created.

Any help will be greatly appreciated

PS: I am using Codeigniter Framework.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongweicha6077 2014-12-12 23:33
    关注

    It depends how you are creating that file, a full example would be very helpful.

    But basically you need to set the correct headers and then you can start outputting the result as it gets created, line by line.

    header('Content-Type: application/octet-stream');
    header('Content-Transfer-Encoding: Binary'); 
    header('Content-disposition: attachment; filename="file.txt"'); 
    
    for($i=1;$i<=20;$i++)
    {
        echo $i . "
    ";
        sleep(1);
    }
    
    exit;
    

    The download prompt will be displayed immediately but downloading the file would take at least 20 seconds.

    You should probably set the headers to the correct mime type, depending on what you intend to do.

    Keep in mind that in your case the size of the download isn't available so the user won't know how big the file is or how much there is left to download. This is a drawback and there is basically nothing you can do if the file isn't already created.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失