PHP ftp_append() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_append() adds the contents of a local file to the end of a file that is already on the FTP server. It does not replace the remote file.

This is handy for things like log files or CSV files where you want to keep growing the same remote file. If the remote file does not exist, the server usually creates it. The function was added in PHP 7.2.

Syntax

Call ftp_append() with the values you need. Start with this line.

PHP
$ok = ftp_append($ftp, "logs/app.log", "today.log", FTP_ASCII);
CallWhat it does
ftp_append($ftp, "logs/app.log", "today.log")Appends the local file to the remote file in binary mode.
ftp_append($ftp, "data.csv", "new-rows.csv", FTP_ASCII)Appends text content using ASCII mode.
ftp_append($ftp, "backup.txt", "extra.txt", FTP_BINARY)Appends the file bytes exactly as they are.

Available since PHP 7.2. From PHP 8.1 the first argument is an FTP\Connection object. Use FTP_ASCII for plain text and FTP_BINARY for everything else.

Library declarations
PHP
ftp_append(FTP\Connection $ftp, string $remote_filename, string $local_filename, int $mode = FTP_BINARY): bool

Parameters

ParameterDescription
ftpRequired. The FTP\Connection object returned by ftp_connect() or ftp_ssl_connect().
remote_filenameRequired. The path of the file on the server that you want to add data to.
local_filenameRequired. The path of the local file whose contents will be appended.
modeOptional. The transfer mode, FTP_ASCII or FTP_BINARY. The default is FTP_BINARY.

Return Value

Return ValueDescription
booltrue on success, false on failure.

PHP Examples

Example 1 — Append a Local File to a Remote Log

Add the contents of a small local file to the end of a remote log. The host and login are placeholders.

PHP
<?php
$ftp = ftp_connect("ftp.example.com");
ftp_login($ftp, "demo_user", "demo_pass");

if (ftp_append($ftp, "logs/app.log", "today.log", FTP_ASCII)) {
    echo "Log appended.", PHP_EOL;
} else {
    echo "Could not append.", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. We connect and log in to the server.
  2. ftp_append() sends today. log and adds it to the end of logs/app.log.
  3. FTP_ASCII is used because log files are plain text.
  4. The script prints a message based on the true or false result.

Example 2 — Appending Twice Duplicates the Data

Calling ftp_append() again with the same file adds the same content a second time.

PHP
<?php
$ftp = ftp_connect("ftp.example.com");
ftp_login($ftp, "demo_user", "demo_pass");

$first  = ftp_append($ftp, "notes.txt", "line.txt", FTP_ASCII);
$second = ftp_append($ftp, "notes.txt", "line.txt", FTP_ASCII);

var_dump($first, $second);
echo "Remote size now: ", ftp_size($ftp, "notes.txt"), " bytes", PHP_EOL;

ftp_close($ftp);

How It Works

  1. line. txt is 12 bytes long in this example.
  2. The first call adds 12 bytes to notes. txt.
  3. The second call adds the same 12 bytes again, so both calls return true.
  4. ftp_size() shows the remote file has grown to 24 bytes.

Example 3 — Append Only When the Local File Exists

Check that the local file is there first, so you do not send a missing file to the server.

PHP
<?php
function appendIfExists($ftp, string $remote, string $local): string
{
    if (!file_exists($local)) {
        return "Local file missing: " . $local;
    }

    return ftp_append($ftp, $remote, $local, FTP_ASCII)
        ? "Appended " . $local
        : "Append failed for " . $local;
}

$ftp = ftp_connect("ftp.example.com");
ftp_login($ftp, "demo_user", "demo_pass");

echo appendIfExists($ftp, "log.txt", "today.log"), PHP_EOL;
echo appendIfExists($ftp, "log.txt", "nope.log"), PHP_EOL;

ftp_close($ftp);

How It Works

  1. The helper first uses file_exists() to check the local file.
  2. If it is missing, a message is returned and no FTP command is sent.
  3. Otherwise ftp_append() runs and the helper returns a success or failure message.
  4. The two calls show both outcomes.

Frequently Asked Questions

It uploads a local file and adds its contents to the end of an existing remote file. If the remote file is missing, most servers create it.
ftp_put() replaces the remote file with the local one. ftp_append() keeps the remote content and adds the local content after it.
It was added in PHP 7.2. In older versions you need a workaround such as downloading, joining and uploading the file again.
Use FTP_ASCII for plain text files like logs and CSV. Use FTP_BINARY for images, ZIP files and anything else that must stay exactly the same.
Common reasons are a wrong remote path, missing write permission, a missing local file, or a server that does not support the APPE command.

Next: ftp_cdup()

ftp_cdup() changes the current directory on the FTP server to the parent directory. It is .

ftp_cdup() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful