PHP KML 生成器

发布于 2024-10-17 07:11:40 字数 5702 浏览 4 评论 0原文

我在执行 KML 生成器的下一步时遇到问题。我有它,所以当您选择日期时,它会将日期发送到生成器,并创建一个 KML,然后下载该 KML。它创建该文件,但下载的文件名为generator.php。它包含我所有的 KML 信息,但这并不是我想要的。因此,我需要一些帮助或教程来将文件转换为 .kml,最好是我选择的要下载的名称。到目前为止,这是我的代码: index.php

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

    </body>
</html>

生成器.php

<?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->createElementNS('http://earth.google.com/kml/2.1', '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', 'http://maps.google.com/mapfiles/kml/pal2/icon63.png');
                $restIconNode->appendChild($restHref);
                $restIconstyleNode->appendChild($restIconNode);
                $restStyleNode->appendChild($restIconstyleNode);
                $docNode->appendChild($restStyleNode);

                $barStyleNode = $dom->createElement('Style');
                $barStyleNode->setAttribute('id', 'barStyle');
                $barIconstyleNode = $dom->createElement('IconStyle');
                $barIconstyleNode->setAttribute('id', 'barIcon');
                $barIconNode = $dom->createElement('Icon');
                $barHref = $dom->createElement('href', 'http://maps.google.com/mapfiles/kml/pal2/icon27.png');
                $barIconNode->appendChild($barHref);
                $barIconstyleNode->appendChild($barIconNode);
                $barStyleNode->appendChild($barIconstyleNode);
                $docNode->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->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]);
                        $placeNode->appendChild($descNode);
                        $styleUrl = $dom->createElement('styleUrl', '#barStyle');
                        $placeNode->appendChild($styleUrl);

                        // Creates a Point element.
                        $pointNode = $dom->createElement('Point');
                        $placeNode->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->createElement('coordinates', $coorStr);
                        $pointNode->appendChild($coorNode);
                    }
                    $id = $id + 1;
                }       
                $kmlOutput = $dom->saveXML();
                header('Content-type: application/vnd.google-earth.kml+xml');
                echo $kmlOutput;
                fclose($file);      
            }   
        ?>

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
        <title>TDM KML Generator</title>
        <script type="text/javascript" src="calendarDateInput.js"></script>
    </head>
    <body>
    <form action="generator.php" method="post">
        <script type="text/javascript">DateInput('orderdate', true, 'YYMMDD')</script>
        <input name="submit" type="submit" value="get KML" />
    </form>

    </body>
</html>

generator.php

<?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->createElementNS('http://earth.google.com/kml/2.1', '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', 'http://maps.google.com/mapfiles/kml/pal2/icon63.png');
                $restIconNode->appendChild($restHref);
                $restIconstyleNode->appendChild($restIconNode);
                $restStyleNode->appendChild($restIconstyleNode);
                $docNode->appendChild($restStyleNode);

                $barStyleNode = $dom->createElement('Style');
                $barStyleNode->setAttribute('id', 'barStyle');
                $barIconstyleNode = $dom->createElement('IconStyle');
                $barIconstyleNode->setAttribute('id', 'barIcon');
                $barIconNode = $dom->createElement('Icon');
                $barHref = $dom->createElement('href', 'http://maps.google.com/mapfiles/kml/pal2/icon27.png');
                $barIconNode->appendChild($barHref);
                $barIconstyleNode->appendChild($barIconNode);
                $barStyleNode->appendChild($barIconstyleNode);
                $docNode->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->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]);
                        $placeNode->appendChild($descNode);
                        $styleUrl = $dom->createElement('styleUrl', '#barStyle');
                        $placeNode->appendChild($styleUrl);

                        // Creates a Point element.
                        $pointNode = $dom->createElement('Point');
                        $placeNode->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->createElement('coordinates', $coorStr);
                        $pointNode->appendChild($coorNode);
                    }
                    $id = $id + 1;
                }       
                $kmlOutput = $dom->saveXML();
                header('Content-type: application/vnd.google-earth.kml+xml');
                echo $kmlOutput;
                fclose($file);      
            }   
        ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

草莓酥 2024-10-24 07:11:40

查看 PHP 手册中 header

如果您希望提示用户
保存您正在发送的数据,例如
生成的 PDF 文件,您可以使用
» 提供的 Content-Disposition 标头
推荐的文件名并强制
浏览器显示保存对话框。

<?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'); ?>

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'); ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文