<?php
// connect and login to FTP server
//ftp setup
$ftp_server = "ftp.test.test.co.uk";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$ftp_username='ftp_username';
$ftp_userpass='ftp_userpass';
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
//local DB setup
$servername = "localhost";
$username = "root";
$password = "TEST";
$dbname= "TEST";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//END database connection//
$sql = "SELECT sku,SUM(quantity) as quantity FROM tbl_old_books GROUP BY isbn";
$result = $conn->query($sql);
header("Content-Disposition: attachment; filename=AllOpenOrders.csv");
header("Content-Type: application/csv; ");
// file creation
$file = fopen('php://temp', 'W');
$header = array("SKU","QUANTITY");
fputcsv($file, $header);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
fputcsv($file, $row );
}
}
$remote_path = "/export/AllOpenOrders.csv";
ftp_fput($ftp_conn, $remote_path, $file, FTP_BINARY, 0);
fclose($file);
ftp_close($ftp_conn);
?>
$file
指向您在文件末尾写入的内容。通过ftp_fput
没什么可写的。您可以在写入FTP之前使用rewind($file);
在文件的开头重置文件指针:rewind documentation