为什么浏览器尝试下载我的PHP脚本文件? [英] Why browser is trying to download my php script file?

查看:131
本文介绍了为什么浏览器尝试下载我的PHP脚本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是一个快速 - 我最近写了一个PHP脚本,动态使用DOM API创建XML文件。所以我用这个开头:

Just a quick one - I wrote a php script recently that dynamically creates XML file using API DOM. So I'm using this at the beginning:

$dom = new DOMDocument('1.0', 'UTF-8');

和结束时它看起来是这样的:

And at the end it looks like this:

$server = $_SERVER['DOCUMENT_ROOT'];
$path_to_xml = "$server/project/file.xml"; 
file_put_contents($path_to_xml, $dom->saveXML());

它做我想要的一切,但为什么浏览器尝试下载这个PHP脚本,而不是仅仅运行呢?请有人可以帮我这个。我是pretty确保它的东西很容易。
// -----------------------------------编辑
感谢所有的答复。是的,我要送定制标题,因为它是我动态创建谷歌地图的KML文件。

It does everything I wanted but why browser is trying to download this php script instead of just run it? Please can someone help me with this. I'm pretty sure it's something easy. //-----------------------------------edited Thanks for all replies. Yes I'm sending custom headers because it's google maps kml file that I'm creating dynamically.

标题(内容类型:应用程序/ vnd.google-earth.kml');

header('Content-type: application/vnd.google-earth.kml');

// Creates the root KML element and appends it to the root document.
$node = $dom->createElementNS('http://earth.google.com/kml/2.0', 'kml');
$parNode = $dom->appendChild($node);

莫非是这可能的原因?

Could that be possible cause of this?

推荐答案

如果文件扩展名是的.php 和Web服务器正确配置它将运行它。

If the file extension is .php and your web server correctly configured it will run it.

您指定应用程序/ XXX 内容类型,所以大多数的浏览器将强制下载并使用脚本的名称作为文件名。

You specify an application/xxx content type so most browsers will force a download and use the name of your script as file name.

如果要强制不同的文件名从你的PHP文件名使用:

If you want to force a file name different from your php file name use :

header('Content-Disposition: attachment; filename=your_requested_file.kml');

这篇关于为什么浏览器尝试下载我的PHP脚本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆