Stream FTP upload in chunks with PHP?

送分小仙女□ 提交于 2019-12-03 22:12:52

OK then... This might be what you're looking for. Are you familiar with curl?

CURL can support appending for FTP:

curl_setopt($ch, CURLOPT_FTPAPPEND, TRUE ); // APPEND FLAG

The other option is to use ftp:// / ftps:// streams, since PHP 5 they allow appending. See ftp://; ftps:// Docs. Might be easier to access.

The easiest way to append a chunk to the end of a remote file is to use file_put_contents with FILE_APPEND flag:

file_put_contents('ftp://username:pa‌​ssword@hostname/path/to/file', $chunk, FILE_APPEND);

If it does not work, it's probably because you do not have URL wrappers enabled in PHP.


If you need a greater control over the writing (transfer mode, passive mode, etc), or you cannot use the file_put_contents, 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, $chunk);
rewind($h);

// prevent ftp_fput from seeking local "file" ($h)
ftp_set_option($conn_id, FTP_AUTOSEEK, false);

$remote_path = '/path/to/file';
$size = ftp_size($conn_id, $remote_path);
$r = ftp_fput($conn_id, $remote_path, $h, FTP_BINARY, $size);

fclose($h);
ftp_close($conn_id);

(add error handling)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!