Php 强制下载生成的HTML
我正在创建一个WordPress插件,当按下按钮时,它将生成HTML页面。这是可行的;这个问题与WordPress无关,尽管代码在WP插件中。下一步是提示用户下载/打开创建的文件 根据此处和其他地方的研究,此过程应创建下载/打开提示:Php 强制下载生成的HTML,php,http-headers,savefiledialog,Php,Http Headers,Savefiledialog,我正在创建一个WordPress插件,当按下按钮时,它将生成HTML页面。这是可行的;这个问题与WordPress无关,尽管代码在WP插件中。下一步是提示用户下载/打开创建的文件 根据此处和其他地方的研究,此过程应创建下载/打开提示: /* another process creates an HTML file "somefile.html", and stores it in the plugin folder; that is done with an fopen/fwrite/f
/* 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文件,并且它正确地存储在插件文件夹中(文件位置不是问题,将在最终版本中解决)。然后我看到打开/保存文件对话框
如果我从服务器打开生成的HTML文件,HTML将如预期的那样。但是如果我通过open/save提示符打开生成的文件,我会得到插件设置页面的表示,而不是生成的HTML
我怀疑我需要一个obflush()/flush(),但将这些语句放在标题行之前并不能解决问题
因此,我的问题是“打开/另存为”对话框没有读取存储在服务器上的“somefile.html”。我得到一个带有打开对话框的插件设置HTML页面
如何确保通过“打开/保存”对话框打开我创建的HTML文件
(请注意,尽管该代码位于WordPress插件中,但问题并不特定于WordPress。该代码只创建了一个表单按钮;在提交时,表单操作创建一个HTML文件并将其保存到服务器。然后使用“header”语句创建一个保存/打开对话框。)
已添加
此代码应显示该过程。这是用于创建somefile.HTML文件的“有效”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;
这是包含一些内容的页面。它将创建HTML页面(“xoutput”内容)。
新尝试和测试:
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;
}
新尝试和测试:
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;
}
您可以通过浏览器访问somefile.html吗?我可以看到服务器上的somefile.html就是我生成的文件(通过我的代码编辑器)。问题是打开对话框中显示的文件是插件页面,而不是生成的文件。不,我的意思是,使用浏览器,可以通过url打开somefile.html吗?我试着远离CMS,但我记得有时访问内部文件夹受到htaccess文件和诸如此类的限制。是的。如果我用代码创建指向生成文件的href链接(或者手动打开该文件,因为我知道它的位置,并且管理员有权访问该内部文件夹),则内容与预期一样。您能告诉插件设置页面来自何处吗?是否可能是somefile.html所在文件夹中的index.html或index.php?能否通过浏览器访问somefile.html?我可以看到服务器上的somefile.html就是我生成的文件(通过我的代码编辑器)。问题是打开对话框中显示的文件是插件页面,而不是生成的文件。不,我的意思是,使用浏览器,可以通过url打开somefile.html吗?我试着远离CMS,但我记得有时访问内部文件夹受到htaccess文件和诸如此类的限制。是的。如果我用代码创建指向生成文件的href链接(或者手动打开该文件,因为我知道它的位置,并且管理员有权访问该内部文件夹),则内容与预期一样。您能告诉插件设置页面来自何处吗?是否可能是somefile.html所在文件夹中的index.html或index.php?是的,可以。谢谢你给我额外的时间!虽然上面的代码可以工作(谢谢,@deg),但是出现了一个bug:请看。是的,这样就可以了。谢谢你给我额外的时间!虽然上面的代码可以工作(谢谢,@deg),但是出现了一个bug:请参阅。