Skip to content

Connecting to FTP Server using ABAP

July 23, 2008

There are several ways can be used to interact your SAP system with other application. If you specifically need an ABAP program to connect to an FTP server, then you are lucky, SAP provides a function module for FTP connection.

Using SAP function module, you can execute any FTP Command. Furthermore you can directly send your internal table content to FTP server as file, and vice versa.

Establishing Connection

To establish a connection use the FTP_CONNECT function. Parameters need for this function are user name, password, FTP Host and RFC Destination. Note that you should encrypt the password before using it as the function parameter.

There are two types of RFC Destination

  • Use SAPFTP
    If program needs to be executed on the client front-end workstation. The program will user RFC Server includes in your client workstation during the gui installation.
    For example, if you need to upload data from client workstation directly to the remote system SAPFTP is best used, thus the process doesn’t use SAP memory to save data temporarily.
  • Use SAPFTPA
    If program needs to be executed on the application server. You should use the SAPFTPA destination if you want to upload files from our application server to the remote system.

Executing FTP Command

Use FTP_COMMAND to execute FTP commands. When uploading/downloading files, be sure to set the directory first. T-code AL11 provide list of directories in SAP, that will help you to assign the directory to the right location

You can also use use FTP_R3_TO_SERVER and FTP_SERVER_TO_R3 if you specifically need to load file from/to internal table. Parameters needed are a Handle (this value will be created when you use FTP_CONNECT), filename, internal table, and size. Note that the size used should be the exact size of the internal table. Meaning if the internal table consist of structure, you must calculate the size of each structure and multiply it with the number of line exist in the internal table.

Example Report

SAP provides example reports for connecting to FTP using the function module above. Following report will give you comprehensive example:

  • RSFTP002: Execute FTP Command
  • RSFTP007: Example use of FTP_R3_TO_SERVER and FTP_SERVER_TO_R3

Resources:
SAP Notes 93042
IT Toolbox

2 Comments leave one →
  1. November 2, 2009 3:27 pm

    how to handle ftp errors ?

    • Tresna permalink*
      December 17, 2009 1:29 pm

      Sorry for the late reply.
      Exceptions are available on the function module to handle FTP errors.

      For example:
      call function ‘FTP_COMMAND’
      EXPORTING
      handle = hdl
      command = cmd1
      compress = compress
      TABLES
      data = result
      EXCEPTIONS
      command_error = 1
      tcpip_error = 2
      data_error = 3.

      Use tcode se37 to check available exceptions for each function module.
      Hope this answers your question

Leave a comment