2011-02-11 03:48



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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
        <title>TDM KML Generator</title>
        <script type="text/javascript" src="calendarDateInput.js"></script>
    <form action="generator.php" method="post">
        <script type="text/javascript">DateInput('orderdate', true, 'YYMMDD')</script>
        <input name="submit" type="submit" value="get KML" />



            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->createElementNS('', 'kml');
                $parNode = $dom->appendChild($node);

                // Creates a KML Document element and append it to the KML element.
                $dnode = $dom->createElement('Document');
                $docNode = $parNode->appendChild($dnode);

                // Creates the two Style elements, one for restaurant and one for bar, and append the elements to the Document element.
                $restStyleNode = $dom->createElement('Style');
                $restStyleNode->setAttribute('id', 'restaurantStyle');
                $restIconstyleNode = $dom->createElement('IconStyle');
                $restIconstyleNode->setAttribute('id', 'restaurantIcon');
                $restIconNode = $dom->createElement('Icon');
                $restHref = $dom->createElement('href', '');

                $barStyleNode = $dom->createElement('Style');
                $barStyleNode->setAttribute('id', 'barStyle');
                $barIconstyleNode = $dom->createElement('IconStyle');
                $barIconstyleNode->setAttribute('id', 'barIcon');
                $barIconNode = $dom->createElement('Icon');
                $barHref = $dom->createElement('href', '');
                $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->createElement('Placemark');
                        $placeNode = $docNode->appendChild($node);

                        // Creates an id attribute and assign it the value of id column.
                        $placeNode->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->createElement('description', $content[7]);
                        $styleUrl = $dom->createElement('styleUrl', '#barStyle');

                        // Creates a Point element.
                        $pointNode = $dom->createElement('Point');

                        // 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->createElement('coordinates', $coorStr);
                    $id = $id + 1;
                $kmlOutput = $dom->saveXML();
                header('Content-type: application/');
                echo $kmlOutput;
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dongli5785 dongli5785 10年前

    Have a look at Example 1 in the PHP manual for header:

    If you want the user to be prompted to save the data you are sending, such as a generated PDF file, you can use the » Content-Disposition header to supply a recommended filename and force the browser to display the save dialog.

    <?php // We'll be outputting a PDF
    header('Content-type: application/pdf');
    // It will be called downloaded.pdf
    header('Content-Disposition: attachment; filename="downloaded.pdf"');
    // The PDF source is in original.pdf
    readfile('original.pdf'); ?>
    点赞 评论 复制链接分享