Why browser is trying to download my php script file?
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 he开发者_如何学Goaders 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?
Because your web server is not correctly configured to process PHP scripts.
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.
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.
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.
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');
You might not be sending correct headers:
<?php header ("content-type: text/xml");
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()
.
精彩评论