PHP 生成 XML 文件以在另存为对话框中加载正确的文件类型
我有一个名为 dynamicxml.php
的 PHP 文件,它运行一堆代码、从数据库中提取数据并创建一个 XML 文件。当我右键单击并保存 dynamicxml.php?var=test
的链接时,我需要对话框来选择 xml 作为默认文件类型并将文件重命名为 var.xml:
header("Content-type: text/xml");
echo '<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<blah><blah_blah>'.$_GET['var'].'</blah></blah>';
单击链接时它在浏览器中加载 xml 文件。保存该页面会将其另存为 var.xml。但是右键单击并保存它会将其另存为dynamicxml.php 和php 文件类型。
我应该使用 htaccess 进行操作还是有其他选择?
I have a PHP file called dynamicxml.php
that runs a bunch of code, draws data from a database and creates an XML file. When I right-click and save the link for dynamicxml.php?var=test
I need the dialog box to select xml as the default file type and rename the file to var.xml:
header("Content-type: text/xml");
echo '<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<blah><blah_blah>'.$_GET['var'].'</blah></blah>';
When clicking the link it loads the xml file in the browser. Saving that page saves it as var.xml. But right clicking and saving it saves it as dynamicxml.php and as a php file type.
Should I manipulate things with htaccess or are there other alternatives?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该尝试这样做,因为浏览器通过“另存为..”进行保存的工作方式有所不同。例如 chrome 和 opera 会将您的文件保存为 .htm(可能)。我看到两种可能的方法来解决这种情况(我不会做其中任何一个):
设置 apache 将 xml 作为 php 进行处理。
AddType text/xml php
创建重写规则,将某个目录中的 *.xml 中的所有内容重定向到dynamicxml.php
you shouldn't try to do that because browsers work differently saving through "save as..". for example chrome and opera would save your file as .htm(probably). i see two possible ways to fix the situation(and i wouldn't do either of them):
set apache to process xml as php.
AddType text/xml php
create a rewrite rule to redirect everything from *.xml in some directory to dynamicxml.php