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.