2013-07-02 85 views
-1

我已經搜查了論壇,並發現了幾個帖子,在這個觸摸,但沒有運氣,IM希望有人能幫助PHP保存CSV文件到服務器不在本地

我的文件生成CSV文件,我可以節省到桌面上,我需要的文件保存到我們的網絡服務器的目錄/連城

我一直在試圖

$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']); 
    } 
    } 



?> 

`

+0

由於您的代碼是現在,它會嘗試將該文件寫入'/ httpdocs/citylinkcitylink_ .csv',因爲目錄沒有/之後。 –

+0

檢查寫入權限並驗證您的路徑是否正確 – steven

回答

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%確定,則可以使用相對路徑(相對於,該文件爲)。

相關問題