开发者

PHP generated XML file to load correct file type in the save as dialog

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_bla开发者_StackOverflow社区h>'.$_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?


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

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.xml$ - [NC,L]
    RewriteRule ^.xml$ dynamicxml.php [NC,L]
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜