php.net |  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
Votes:1
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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ndobromirov at gmail dot com
New email:
PHP Version: OS:

 

 [2017-05-12 12:50 UTC] ndobromirov at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/ref.ftp
---

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('ftp://user:pass@exmaple.com:21/test.txt', "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 = 'ftp://user:pass@example.com:21';
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:
----------------
test

test
test

test
test
test


Actual result:
--------------
test

test

test


Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

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] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2018-06-19 20:29 UTC] nikic@php.net
ftp_append() has been added in PHP 7.2.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 16:01:29 2024 UTC