Page 1 of 1

Browser not displaying file

Posted: Sun Oct 31, 2010 6:20 am
by anevins
My PHP file keeps trying to save or open when i view it, even in firefox.
I'm trying to open it on a webserver and localhost but i still get the same pop-up window which asks me to either open it or save it.

The file is index.php:

Code: Select all

<?php

require_once('connectvars.php');

$dbc = mysql_connect ($server, $username, $password);
if(!$dbc){
die('Not connected : ' . mysql_error());
}

$db_selected = mysql_select_db($database, $dbc);
if (!$db_selected){
die ('Cannot use db: ' . mysql_error());
}

$query = 'SELECT * FROM maps WHERE 1';
$result = mysql_query($query);
if (!$result){
die('Invalid query ' . mysql_error());
}

//creates an array of strings to hold the liens of the KML file
$kml = array('<?xml version="1.0" encoding="UTF-8"?>');
$kml[] = '<kml xmlns="http://earth.google.com/kml/2.1">';
$kml[] = ' <Document>';
$kml[] = ' <Icon>';
$kml[] = ' <href>http://maps.google.com/mapfiles/kml/pal2/icon18.png</href';
$kml[] = ' </Icon>';
$kml[] = ' <Icon>';
$kml[] = ' <href>http://maps.google.com/mapfiles/kml/pal2/icon18.png</href';
$kml[] = ' </Icon>';

//Adds note for each row
while ($row = @mysql_fetch_assoc($result)){
$kml[] = ' <Placemark>' . $row['id'] . '">';
$kml[] = ' <city> ' . htmlentities($row['city']) . '</city>';
$kml[] = ' <Point>';
$kml[] = ' <coordinates>' . $row['lng'] . ',' . $row['lat'] . '</coordinates>';
$kml[] = ' </Point>';
$kml[] = ' <Placemark>';
}

//End of XML file
$kml[]= '</Document>';
$kml[]= '</kml>';
$kmlOutput = join("\n", $kml);
header('Content-type: application/vnd.google-earth.kml+xml');
echo $kmlOutput;
?>

Re: Browser not displaying file

Posted: Sun Oct 31, 2010 2:49 pm
by Jonah Bron
The headers aren't setup properly. Sounds like you either didn't install PHP, or it wasn't integrated with Apache properly.

Re: Browser not displaying file

Posted: Tue Nov 02, 2010 3:07 pm
by josh
Web browsers display text/html, text/css text/javascript, etc... You are using google earth KML file. Why *wouldn't* it prompt you to save/open?

Re: Browser not displaying file

Posted: Tue Nov 02, 2010 3:22 pm
by Jonah Bron
Hehe, didn't even look at the code :oops:

If you want to be able to view your KML in the browser, change the header() method to this:

Code: Select all

header('Content-type: text/plain');