更新时间:2022-01-05 21:23:21
file_put_contents
是最简单的解决方案:
The file_put_contents
is the easiest solution:
file_put_contents('ftp://username:password@hostname/path/to/file', $contents);
如果它不起作用,可能是因为您没有启用 URL 包装器PHP.
If it does not work, it's probably because you do not have URL wrappers enabled in PHP.
如果您需要更好地控制写入(传输模式、被动模式、偏移量、读取限制等),请使用 ftp_fput
带有 php://temp
(或 php://memory
)流:
If you need greater control over the writing (transfer mode, passive mode, offset, reading limit, etc), use the ftp_fput
with a handle to the php://temp
(or the 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 服务器上打开/创建文件.如果文件很大,这特别有用,因为您不会将全部内容保存在内存中.
Or you can open/create the file directly on the FTP server. That's particularly useful, if the file is large, as you won't have keep whole contents in memory.