Having difficulty with a process that creates HTML page code and then display the
Open/Save As dialog. This code is used on two different sites, and works properly
on one site (showing the Open/Save As dialog as desired) but the other site shows
the HTML code inline on the calling page (the process is called by a form button).
The code creates a full HTML page (html/head/body/etc), in the code below
that HTML content is $xoutput
variable.
The $thefile
is the fully-pathed file name.
I have tried many different combinations of the header()
settings. Note again
that the code works properly on one site, but the exact same code shows
the HTML content inline, rather than the Open/Save As dialog. There is no
output before the header() that might cause a 'headers already sent' error
(which doesn't happen).
Both sites are running PHP 5.4.43. Both sites have the exact same code.
Note that the problem also occurs on another (customer) site. Both sites
running in the same browser (latest Firefox). Same things on both sites, but different results. Both sites are WordPress(latest); disabling all other plugins and using 2015 theme still shows inline HTML on the problem site.
Why would the code work properly on one site, but not the other? The problem
is somewhere in the header()
statements, I think, but that's all I can figure out. Have tried lots of code suggested by the googles. The only variable between the two sites is the two sites. All else is the same.
The code below is based on my previous question on the same subject (here https://stackoverflow.com/questions/46019470/force-download-of-generated-html )
function output_file($post_output = "nothing found")
{
$thefile = 'myfile.html' ;
$size = strlen($post_output);
header("Content-Description: File Transfer") ;
header("Content-Type: application/octet-stream") ;
header("Content-Disposition: attachment; filename=$thefile");
header("Content-Transfer-Encoding: binary") ;
header("Expires: 0") ;
header("Cache-Control: must-revalidate, post-check=0, pre-check=0") ;
header("Pragma: public") ;
header("Content-Length:" . $size);
ob_clean() ;
flush() ;
echo $post_output;
exit ;
return ;
}
Thanks...Rick...