The file_put_contents是最简单的解决方案:
file_put_contents('ftp://username:password@hostname/path/to/file', $contents);
如果不起作用,可能是因为您没有PHP 中启用的 URL 包装器.
如果您需要更好地控制写入(传输模式、被动模式、偏移、读取限制等),请使用ftp_fput带有一个手柄php://temp(或者php://memory) stream:
$conn_id = ftp_connect('hostname');
ftp_login($conn_id, 'username', 'password');
ftp_pasv($conn_id, true);
$h = fopen('php://temp', 'r+');
fwrite($h, $contents);
rewind($h);
ftp_fput($conn_id, '/path/to/file', $h, FTP_BINARY, 0);
fclose($h);
ftp_close($conn_id);
(添加错误处理)
或者您可以直接在 FTP 服务器上打开/创建文件。如果文件很大,这特别有用,因为您不需要将整个内容保留在内存中。
See 使用 PHP 在外部 FTP 服务器上生成 CSV 文件.