PHP将CSV文件保存到服务器而不是本地

时间:2013-07-02 17:05:54

标签: php csv

我搜索了论坛,发现了一些涉及此事的帖子,但没有运气,我希望有人可以提供帮助

我的文件生成一个CSV文件,我可以保存到桌面,我需要将文件保存到我们的webserver / citylink上的目录

我一直在尝试

$filename="citylink";  $directory = "/httpdocs/citylink";
$csv_filename = $filename."_".date("Y-m-d_H-i",time()).".csv";

$fd = fopen ("$directory" . $csv_filename, "w");

echo fputs($fd);

fclose($fd);

但是没有运气,我确定我没有把它放在正确的位置..

非常感谢任何帮助

先谢谢你

罗素

完整

`

<?php

require('includes/application_top.php');

// csv settings

define("CSV_SEPARATOR", ",");
define("CSV_NEWLINE", "\r\n");


//csv dump info

$filename="citylink";  $directory = "/httpdocs/citylink";
$csv_filename = $filename."_".date("Y-m-d_H-i",time()).".csv";

$fd = fopen ("$directory" . $csv_filename, "w");

echo fputs($fd);

fclose($fd);


// not submitted, so show form to submit

if (! $_POST['submitted'] && ! $_POST['submitted' == 1 ] )  {
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<link rel="stylesheet" type="text/css" href="includes/cssjsmenuhover.css" media="all" id="hoverJS">
<script language="javascript" src="includes/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
<script type="text/javascript">
  <!--
  function init()
  {
    cssjsmenu('navbar');
    if (document.getElementById)
    {
      var kill = document.getElementById('hoverJS');
      kill.disabled = true;
    }
  }
  // -->
</script>
</head>
<body onLoad="init()">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr>
<td>
  <div class="pageHeading"><?php echo 'City Link - Send Jobs To Xtend'; ?></div>

<br><br>

  <form method="post" action="<?php echo $PHP_SELF;?>">
  <table border="0" cellpadding="3">
  <tr>
    <td><?php echo 'Start From Invoice'; ?></td>
    <td><input name="start" size="5" value="<?php echo (int)$_POST['start']; ?>">
  </tr>
  <tr>
    <td><?php echo 'End Invoice'; ?></td>
    <td><input name="end" size="5" value="<?php echo (int)$_POST['end']; ?>">
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" value="<?php echo 'Send File To X-tend'; ?>"></td>
  </tr>
  </table>
  <input type="hidden" name="submitted" value="1">
  </form>

</td>
  </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php
}
// submitted so generate csv
else
{
  generatecsv((int)$_POST['start'], (int)$_POST['end']);

}
require(DIR_WS_INCLUDES . 'application_bottom.php');


// generates csv file from $start order to $end order, inclusive
function generatecsv($start, $end)
{
  global $db;

  $sql =  "SELECT * FROM " . TABLE_ORDERS . "
           WHERE orders_id >= :start:
           AND   orders_id <= :end:";
  $sql = $db->bindVars($sql, ':start:', $start, 'integer');
  $sql = $db->bindVars($sql, ':end:',   $end,   'integer');
  $orders = $db->Execute($sql);

  header("Pragma: cache");
  header("Content-Type: text/comma-separated-values");
  header("Content-Disposition: attachment; filename=" . 'city_link_export.csv');


  while (!$orders->EOF)
  {
    $codes_lookup = get_country_and_zone_code($orders->fields['delivery_country'], $orders->fields['delivery_state']);
    {
      list($country, $province) = $codes_lookup;


      //CSV OUTPUT FILE


      echo quote($orders->fields['orders_id']).CSV_SEPARATOR;
      echo quote($orders->fields['customers_id']).CSV_SEPARATOR;
      echo quote($orders->fields['delivery_name']).CSV_SEPARATOR;
      echo quote($orders->fields['delivery_street_address']).CSV_SEPARATOR;
      echo quote($orders->fields['delivery_suburb']).CSV_SEPARATOR;
      echo quote('').CSV_SEPARATOR;   // Address Line3
      echo quote($orders->fields['delivery_city']).CSV_SEPARATOR;
      echo quote($orders->fields['delivery_state']).CSV_SEPARATOR;
      echo quote($orders->fields['delivery_postcode']).CSV_SEPARATOR;
      echo quote($orders->fields['customers_telephone']).CSV_SEPARATOR;
      echo quote('').CSV_SEPARATOR;  //email address not included becauce of amazon emails
      echo quote($orders->fields['date_purchased']).CSV_SEPARATOR;
      echo quote('107').CSV_SEPARATOR;  //service level
      echo quote('9').CSV_SEPARATOR;   // default pacel weight
      echo quote('1').CSV_SEPARATOR;   // default no of parcels
      echo quote('J1').CSV_SEPARATOR;   // default package description
      echo quote($orders->fields['delivery_name']).CSV_SEPARATOR;
      echo quote('').CSV_SEPARATOR;   // bulk name
      echo quote('').CSV_SEPARATOR;   // consignment number
      echo quote('8108991').CSV_SEPARATOR;   // city link account number
      echo quote('').CSV_SEPARATOR;   // special delivery instructions
      echo quote('OYPLA').CSV_SEPARATOR;   // Description of goods (mandatory for Irish deliveries
      echo quote('').CSV_SEPARATOR;   // printer mappinng
      echo quote('N').CSV_SEPARATOR;   // saturday delivery Y/N
      echo quote('N').CSV_SEPARATOR;   // return shipment Y/N
      echo quote('').CSV_SEPARATOR;   // default payment on delivery ammount
      echo quote('').CSV_SEPARATOR;   // default value of goods ammount
      echo quote('').CSV_SEPARATOR;   // PDN, pre delivery notification
      echo quote('N').CSV_SEPARATOR;   // Exchange Marker

      echo quote().CSV_NEWLINE;

    }



    $orders->MoveNext();

  }
}

// returns the name for a shipping status
function getorderstatus($statusid)
{
  global $db;
  $query = "select * from " . TABLE_ORDERS_STATUS . " where orders_status_id = $statusid";
  $statii = $db->Execute($query);
  while (!$statii->EOF) {
    return $statii->fields['orders_status_name'];
  }

  return $statusid;
}

// formats a value suitable for including in a csv file
function quote($value)
{
  // if quote mark in string then escape with another quote mark
  // then put inside quote marks and return
  if (strstr($value, '"') !== FALSE)
  {
    $value = str_replace('"', '""', $value);
    return '"$value"';
  }

  // if separator in string then put inside quote marks
  if (strstr($value, CSV_SEPARATOR) !== FALSE)
  {
    return '"$value"';
  }

  return $value;
}

  function get_country_and_zone_code($country_name, $zone_name) {
    global $db;
    $sql = "select countries_id, countries_name, countries_iso_code_2, countries_iso_code_3
            from " . TABLE_COUNTRIES . "
            where countries_name = :ctryname: ";
    $sql = $db->bindVars($sql, ':ctryname:', $country_name, 'string');
    $result1 = $db->Execute($sql);
    if ($result1->RecordCount() == 0) return 'BAD';
    $sql = "select zone_code, zone_id, zone_name
            from " . TABLE_ZONES . "
            where zone_country_id = :ctryid:
            and zone_name = :zonename:";
    $sql = $db->bindVars($sql, ':ctryid:', $result1->fields['countries_id'], 'integer');
    $sql = $db->bindVars($sql, ':zonename:', $zone_name, 'string');
    $result2 = $db->Execute($sql);

    if ($result2->RecordCount() == 0) {
      return 'BAD';
    } else {
      return array($result1->fields['countries_iso_code_2'], $result2->fields['zone_code']);
    }
  }



?>

`

1 个答案:

答案 0 :(得分:1)

这部分错了:

$filename="citylink";  $directory = "/httpdocs/citylink";
$csv_filename = $filename."_".date("Y-m-d_H-i",time()).".csv";

$fd = fopen ("$directory" . $csv_filename, "w");

您在没有目录分隔符的情况下连接$directory$csv_filename。这将导致:/httpdocs/citylinkcitylink_Y-m-d_H-i.csv - 这可能是错误的。

使用:$fd = fopen ("$directory/$csv_filename", "w");

其次,您需要确保$directory是正确的绝对路径。如果您不是100%确定,则可以使用相对路径(相对于 THIS 文件)。