douganbi7686 2017-09-03 01:16
浏览 32
已采纳

强制下载生成的HTML

I am creating a WordPress plugin that will generate HTML page when a button is pressed. That works; the issue is not WordPress-related, although the code is in a WP plugin. The next step is to give the user a prompt to download/open the file that was created.

Based on research here and elsewhere, this process should create a download/open prompt:

   /* another process creates an HTML file "somefile.html", and stores it
 in the plugin folder; that is done with an fopen/fwrite/fclose.
 This process is started by a button on the plugin settings page.
 When the button is clicked, the "somefile.html" is created. 
So at this point, the HTML file is created and stored in the plugin folder */

    $size   = filesize($thefile);
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='somefile.html'); 
    header('Content-Transfer-Encoding: binary');
    header('Connection: Keep-Alive');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . $size);
    exit;

The process (started by the button click on the plugin settings page) does create the HTML file, and it is stored properly in the plugin folder (file location is not an issue and will be addressed in the final version). Then I see the open/save file dialog.

If I open the generated HTML file from the server, the HTML is as expected. But if I open the generated file via the open/save prompt, I get a representation of the plugin settings page, not the generated HTML.

I suspect that I need an obflush()/flush(), but placing those statements before the header lines does not fix the problem.

So my issue is that the open/save as dialog does not read the 'somefile.html' that was stored on the server. I get a plugins settings HTML page with the open dialog.

How do I ensure that the HTML file that I create is opened via the open/save dialog?

(Note that although the code is inside a WordPress plugin, the problem is not specific to WordPress. The code just creates a form button; on submit the form action creates an HTML file and saves it to the server. The 'header' statements are then used to create a save/open dialog.)

ADDED

This code should show the process. This is the 'effective' HTML page used to create the somefile.html file.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>
    <body>
    This is the page with some content. It will create the HTML page (the 'xoutput' content).  
    </body>
<!--- the above is the page that is initially displayed -->
    <?php
     // now we create the content of the generated/saved file    
    $xoutput = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>
    <body>';
    $xoutput .=  'There is some content generated here. Actual content doesn't matter.';
    $xoutput .=  '</body> </html>';
        $thefile = "outputfile.html";
        $handle = fopen($thefile, "w");
        fwrite($handle, $xoutput);
        fclose($handle);
    $quoted = sprintf('"%s"', addcslashes(basename($thefile), '"\\'));
    $size   = filesize($thefile);
    // now that the somefile.html has been created and stored, let's create the open/save dialog
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . $quoted); 
    header('Content-Transfer-Encoding: binary');
    header('Connection: Keep-Alive');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . $size);

        exit;
        return;

When you load the page, you get the 'There is some content generated here' HTML page, not the 'somefile.html' content.

  • 写回答

1条回答 默认 最新

  • doujiejujixi27244 2017-09-03 04:03
    关注

    Freshly Tried And Tested:

    if (file_exists($thefile)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($thefile));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: '.filesize($thefile));
        ob_clean();
        flush();
        readfile($thefile);
        exit;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP