|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #74582 Can not append to remote files with the ftp_* API
Submitted: 2017-05-12 12:50 UTC Modified: 2018-06-19 20:29 UTC
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: ndobromirov at gmail dot com Assigned: nikic (profile)
Status: Closed Package: FTP related
PHP Version: 7.1.5 OS: All
Private report: No CVE-ID: None
 [2017-05-12 12:50 UTC] ndobromirov at gmail dot com
From manual page:

This was tested against 5.6, but the ftp_append method is not present in PHP 7 too.

Developers can append to FTP files through the streaming interfaces, but this is creating new FTP connections every time:
file_put_contents('', "test\n", FILE_APPEND);

The same is not possible through the dedicated FTP API provided with all of the ftp_* functions. There is no ftp_append function.
And aparently this is also not possible with ftp_put and using the last parameter - offset (see test script).

Test script:
// Init...
$ftp_connection = '';
list ($source, $download) = ['test.txt', 'download.txt'];
file_put_contents($source, "test\n");

// Connecnt...
$remote = '/test_remote.txt';
$dsn = parse_url('ftp://user:pass@ftp:21');
if (!(($ftp = ftp_connect($dsn['host'], $dsn['port'])) !== false && ftp_login($ftp, $dsn['user'], $dsn['pass']))) {
    die('No log-in');

// Test...
@ftp_delete($ftp, $remote);
for ($i = 0; $i < 3; ++$i) {
    if (ftp_put($ftp, $remote, $source, FTP_BINARY, $i ? ftp_size($ftp, $remote) : 0)) {
        ftp_get($ftp, $download, $remote, FTP_BINARY);
        echo file_get_contents($download), "\n";

Expected result:



Actual result:




Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2017-07-09 01:14 UTC] gmblar+php at gmail dot com
You cannot use $startpos in ftp_put to append data to an existing file, because the $startpos seek the position in the local and the remote file.
 [2018-06-19 20:29 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2018-06-19 20:29 UTC]
ftp_append() has been added in PHP 7.2.
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Thu Jun 21 12:01:57 2018 UTC