正在 PHP FTP 服务器中更新 CSV 文件

Updating CSV file in FTP server in PHP

我正在尝试更新外部 FTP 服务器中的 CSV 文件,我尝试遵循基本的 ftp_fput() 但它不起作用。当我 运行 这个不需要的脚本时,文件没有更新,并且正在下载一个空白的 CSV 文件。我一直在尝试解决这个问题,但找不到解决方案

<?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