views:

101

answers:

7

Hi there everyone. 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?

+5  A: 

Because your web server is not correctly configured to process PHP scripts.

Ignacio Vazquez-Abrams
Thanks for the hint. Could you please provide more details on that? What do I have to change in my server config?
Pavel
@Pavel Please answer the comment questions first. Is PHP installed at all? What server are you using?
Pekka
It is apache and php is installed because I'm running other script without any problems.
Pavel
A: 

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');
VirtualBlackFox
Thanks Virtual for the answer but this is not what I wanted. The thing is I don't want to download anything here. I just want to create the .kml file in the background and put it in the folder on the server and that's it - no downloading
Pavel
So, why are you specifying a Content-Type to the USER BROWSER via header() if you don't want that ??????? You tell the browser that you will send him a file, he download it, that's as simple as that even if you send him nothing he will download a 0k file. If you don't want him to download the file just don't send this header.
VirtualBlackFox
A: 

I have experienced that it happens due to some error in the .htaccess, it may seem absurd because i did not mentioned anything to see in the .htaccess. You should take a closer look in to it.

Imran Naqvi
A: 

If the browser is trying to download the PHP source, then this means that Apache was either not configured to run the PHP interpreter and/or, if you are using a Linux, Unix, or Mac OS X operating system, Apache did not have permission to execute the PHP script.

You need to follow the instructions at Manually Configure PHP5 and Apache2 to make sure that your httpd.conf is correct.

If, in addition, you are running Apache on Linux, Unix, or Mac OS X, then you need to open a terminal, cd into the directory that contains your PHP script, and:

chmod a+x SCRIPT.php

where SCRIPT.php is the name of your PHP script.

Daniel Trebbien
A: 

You might not be sending correct headers:

<?php header ("content-type: text/xml");
Richard Knop
+1  A: 

I'm assuming that this command sends a header:

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

If you're writing a file on the server and not intending to send any response to the client, why are you sending a header to the client?

If your PHP file is just supposed to write a file on the server and do nothing else, don't send a header or indeed anything else to the client.

If that doesn't help, try rephrasing your question. You have received a wide variety of responses so far to all manner of different problems.

Mark Chorley
A: 

My guess is that the browser is not trying to download the PHP script, but IS trying to download the KML file. Comment out the header() line and see if it works. When saving a file locally, you should not need to include header().

Joseph
Too slow again. Mark got it first.
Joseph