PHP Function Reference

PHP ftp_append() Function



The PHP ftp_append() function is used to append the contents of a file to another file on the FTP server.

Syntax

ftp_append(ftp, server_file, local_file, mode)

Parameters

ftp Required. Specify the FTP connection to use.
server_file Required. Specify the server file path where the content need to be appended.
local_file Required. Specify the path of the file from where the content is taken to append on the FTP server.
mode Optional. Specify the transfer mode. Default is FTP_BINARY.

Return Value

Returns true on success or false on failure.

Example:

The example below shows the usage of ftp_append() function.

<?php
//FTP server to use
$ftp_server = "ftp.example.com";
  
//username for the FTP Connection
$ftp_user = "user";
  
//password for the user
$ftp_pass = "password";
  
//set up a connection or die 
$ftp = ftp_connect($ftp_server)
    or die("Could not connect to $ftp_server");
   
if($ftp) {
  echo "Successfully connected to $ftp_server!\n";
 
  //trying to login
  if(@ftp_login($ftp, $ftp_user, $ftp_pass)) {
    echo "Connected as $ftp_user@$ftp_server\n";

    //server file path where the 
    //content need to be appended
    $server_file = "server_demo.txt";

    //local file path from where the 
    //content is taken to append
    $local_file = "local_demo.txt";
        
    //appending the content of specified local file
    if (ftp_append($ftp, $server_file, 
                $local_file, FTP_BINARY)) {
      echo "Successfully appended the content\n";
    } else {
      echo "Error while appending\n";
    }
    
  } else {
    echo "Couldn't connect as $ftp_user\n";
  }
 
  //close the connection
  if(ftp_close($ftp)) {
    echo "Connection closed successfully!\n"; 
  } 
}
?>

The output of the above code will be:

Successfully connected to ftp.example.com!
Connected as user@ftp.example.com
Successfully appended the content
Connection closed successfully!

❮ PHP FTP Reference