dongxiejie9387 2010-07-06 10:07 采纳率: 100%
浏览 79
已采纳

为什么浏览器试图下载我的PHP脚本文件?

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());

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.

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?

  • 写回答

7条回答 默认 最新

  • douhao3562 2010-07-06 10:49
    关注

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

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

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

    header('Content-Disposition: attachment; filename=your_requested_file.kml');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试