在PHP中使用ftp读取文件

时间:2015-01-09 15:07:11

标签: php ftp

我正在尝试使用php在ftp服务器中读取csv文件。 以下是我的代码: - maps.php

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Circles</title>
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>

   <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
    <script>
// This example creates circles on the map, representing
// populations in North America.

// First, create an object containing LatLng and population for each city.

    var citymap = [];
    var temp = [];
    <?php
        $data = array();
        $file = fopen("ftp://b7_15716007:xxxxxx@ftp.byethost7.com/htdocs/data.csv","r");
    while(!feof($file))
    {
        $data = fgetcsv($file);
    ?>
        temp.push("<?php echo $data[0]; ?>");
        temp.push("<?php echo $data[1]; ?>");
        temp.push("<?php echo $data[2]; ?>");
        temp.push("<?php echo $data[3]; ?>");
        citymap.push(temp);
        console.log(citymap);
        temp = [];
    <?php
    }
    fclose($file);
    ?>
var cityCircle;

function initialize() {
  // Create the map.
  var mapOptions = {
    zoom: 4,
    center: new google.maps.LatLng(37.09024, -95.712891),
    mapTypeId: google.maps.MapTypeId.TERRAIN
  };

  var map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);

  // Construct the circle for each value in citymap.
  // Note: We scale the area of the circle based on the population.
      var fillcolor=[];
      fillcolor[0]='#FF0000';fillcolor[1]='#FFFF00'; fillcolor[2]='#FF00FF';    fillcolor[3]='#00FF00';
      var loop=0;
     for (i = 0; i < citymap.length; i++) {  
    var populationOptions = {
      strokeColor: '#000000',
      strokeOpacity: 0.8,
      strokeWeight: 2,

      fillColor: fillcolor[loop],
      fillOpacity: 0.35,
      map: map,
      center: new google.maps.LatLng(citymap[i][1], citymap[i][2]),
      radius: Math.sqrt(citymap[i][3]) * 100000
    };

    // Add the circle for this city to the map.
    cityCircle = new google.maps.Circle(populationOptions);
   // cityCircle = new google.maps.Circle(populationOptions1);
    loop=loop+1;


  }
}

google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

此代码必须读取csv中的值并在google map api中表示它们。 但是代码不起作用。 任何人都可以帮我解决这个问题。 csv的链接是 即使在通过身份验证的URL后,我也没有显示地图。

1 个答案:

答案 0 :(得分:0)

您不能只打开受密码保护的ftp资源..您必须使用密码才能进入。

请尝试使用一些身份验证功能,例如:

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
    echo "Successfully written to $local_file\n";
} else {
    echo "There was a problem\n";
}

有关详细信息,请参阅:http://php.net/manual/en/function.ftp-get.php