强制下载生成的 HTML

Force Download of generated HTML

我正在创建一个 WordPress 插件,它会在按下按钮时生成 HTML 页面。这样可行;问题不是 WordPress-related,尽管代码在 WP 插件中。下一步是提示用户 download/open 创建的文件。

根据此处和其他地方的研究,此过程应创建一个 download/open 提示:

   /* 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;

该过程(通过单击插件设置页面上的按钮启动)确实创建了 HTML 文件,并且它正确存储在插件文件夹中(文件位置不是问题,将在最终版本)。然后我看到 open/save 文件对话框。

如果我从服务器打开生成的 HTML 文件,HTML 是预期的。但是如果我通过 open/save 提示符打开生成的文件,我得到的是插件设置页面的表示,而不是生成的 HTML.

我怀疑我需要一个 obflush()/flush(),但是将这些语句放在 header 行之前并不能解决问题。

所以我的问题是 open/save 作为对话框没有读取存储在服务器上的 'somefile.html'。我得到一个带有打开对话框的插件设置 HTML 页面。

如何确保通过 open/save 对话框打开我创建的 HTML 文件?

(请注意,虽然代码在 WordPress 插件中,但问题并非特定于 WordPress。代码只是创建一个表单按钮;在提交时,表单操作会创建一个 HTML 文件并将其保存到服务器。然后使用 'header' 语句创建 save/open 对话框。)

已添加

此代码应显示该过程。这是用于创建 somefile.html 文件的 'effective' HTML 页面。

    <!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;

加载页面时,您会看到 'There is some content generated here' HTML 页面,而不是 'somefile.html' 内容。

新鲜试用和测试:

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;
}