title: PHP KML Generator
body: <p>I am having having trouble with the next step of my KML generator. I have it so when you choose a date it will send the date to the generator and it will create a KML that will then be downloaded. It creates the file but the file that is downloaded is called generator.php. It has all my KML information in it, but that is not quite what I was wanting. So I need a little help or a tutorial on getting the file to be a .kml with preferably a name of my choosing that that will be downloaded. Here is my code so far:
  index.php</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
      &lt;head&gt;
          &lt;meta content="text/html; charset=utf-8" http-equiv="Content-Type" /&gt;
          &lt;title&gt;TDM KML Generator&lt;/title&gt;
          &lt;script type="text/javascript" src="calendarDateInput.js"&gt;&lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
      &lt;form action="generator.php" method="post"&gt;
          &lt;script type="text/javascript"&gt;DateInput('orderdate', true, 'YYMMDD')&lt;/script&gt;
          &lt;input name="submit" type="submit" value="get KML" /&gt;
      &lt;/form&gt;
  
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>generator.php</p>
  
  <pre><code>&lt;?php
              if($_SERVER['REQUEST_METHOD'] == 'POST')
              {
                  $date = $_POST['orderdate'];
                  $file = fopen("http://www.xxxxxxxxxxxxxxxxxxx/".$date."xxxxxxxxx.csv", "r");
                  $content = fgetcsv($file, 1000, ",");
                  $dom = new DOMDocument('1.0', 'UTF-8'); 
  
                  // Creates the root KML element and appends it to the root document.
                  $node = $dom-&gt;createElementNS('http://earth.google.com/kml/2.1', 'kml');
                  $parNode = $dom-&gt;appendChild($node);
  
                  // Creates a KML Document element and append it to the KML element.
                  $dnode = $dom-&gt;createElement('Document');
                  $docNode = $parNode-&gt;appendChild($dnode);
  
                  // Creates the two Style elements, one for restaurant and one for bar, and append the elements to the Document element.
                  $restStyleNode = $dom-&gt;createElement('Style');
                  $restStyleNode-&gt;setAttribute('id', 'restaurantStyle');
                  $restIconstyleNode = $dom-&gt;createElement('IconStyle');
                  $restIconstyleNode-&gt;setAttribute('id', 'restaurantIcon');
                  $restIconNode = $dom-&gt;createElement('Icon');
                  $restHref = $dom-&gt;createElement('href', 'http://maps.google.com/mapfiles/kml/pal2/icon63.png');
                  $restIconNode-&gt;appendChild($restHref);
                  $restIconstyleNode-&gt;appendChild($restIconNode);
                  $restStyleNode-&gt;appendChild($restIconstyleNode);
                  $docNode-&gt;appendChild($restStyleNode);
  
                  $barStyleNode = $dom-&gt;createElement('Style');
                  $barStyleNode-&gt;setAttribute('id', 'barStyle');
                  $barIconstyleNode = $dom-&gt;createElement('IconStyle');
                  $barIconstyleNode-&gt;setAttribute('id', 'barIcon');
                  $barIconNode = $dom-&gt;createElement('Icon');
                  $barHref = $dom-&gt;createElement('href', 'http://maps.google.com/mapfiles/kml/pal2/icon27.png');
                  $barIconNode-&gt;appendChild($barHref);
                  $barIconstyleNode-&gt;appendChild($barIconNode);
                  $barStyleNode-&gt;appendChild($barIconstyleNode);
                  $docNode-&gt;appendChild($barStyleNode);
                  $id = 1;
                  while (($content = fgetcsv($file, 1000, ",")) !== FALSE) {
  /*******************************************************************************************
                      Values of content
                      (ignore)****content[0] = Time*******(ignore)
                                  content[1] = Size
                      (ignore)****content[2] = Location***(ignore)
                                  content[3] = City
                                  content[4] = State
                                  content[5] = Lat 
                                  content[6] = Long
                                  content[7] = Comments
  *******************************************************************************************/
                      if ($content !== false) {
  
                          $node = $dom-&gt;createElement('Placemark');
                          $placeNode = $docNode-&gt;appendChild($node);
  
                          // Creates an id attribute and assign it the value of id column.
                          $placeNode-&gt;setAttribute('id', 'placemark' . $id);
  
                          // Create name, and description elements and assigns them the values of the name and address columns from the results.
                          $descNode = $dom-&gt;createElement('description', $content[7]);
                          $placeNode-&gt;appendChild($descNode);
                          $styleUrl = $dom-&gt;createElement('styleUrl', '#barStyle');
                          $placeNode-&gt;appendChild($styleUrl);
  
                          // Creates a Point element.
                          $pointNode = $dom-&gt;createElement('Point');
                          $placeNode-&gt;appendChild($pointNode);
  
                          // Creates a coordinates element and gives it the value of the lng and lat columns from the results.
                          $coorStr = $content[6] . ','  . $content[5];
                          $coorNode = $dom-&gt;createElement('coordinates', $coorStr);
                          $pointNode-&gt;appendChild($coorNode);
                      }
                      $id = $id + 1;
                  }       
                  $kmlOutput = $dom-&gt;saveXML();
                  header('Content-type: application/vnd.google-earth.kml+xml');
                  echo $kmlOutput;
                  fclose($file);      
              }   
          ?&gt;
  </code></pre>