Downtime Announcement: Please note the SAP Community Wiki will be unavailable due to a system upgrade on Thursday, September 24th between 6 and 7 AM CEST
Skip to end of metadata
Go to start of metadata

FTP

File transfer protocol is a standard network protocol used to exchange and manipulate files of TCP/IP based network such as the internet. FTP is built on a client server architecture and utilizes separate control and data connections between the client and the server applications. FTP is often used as an application component to automatically transfer files for program internal functions. FTP can be used with user-based password authentication or with anonymous user access. Trivial File transfer Protocol (TFPT) is an unauthenticated version of FTP.

FTP from ABAP

There are some function modules which are used to work with FTP server in ABAP. First a connection needs to be established between SAP and the FTP serer. To achieve this we need to use the function module FTP_CONNECT. The parameters which need to be passed to this function module are

  • USER - User ID on the FTP server using which the connection will be authenticated.
  • PASSWORD - Password of the user to be connected
  • HOST - This could be the server name or an IP address to which one wants to connect
  • RFC Destination - An RFC destination defined (Generally is SAPFTP)
  • This returns HANDLE. This handle is used for further commands.

Before call to the FTP_CONNECT, the password needs to be scrambled for security reasons. To do the same we use the function module HTTP_SCRAMBLE. Pass the parameters password in SOURCE, length off the password to SOURCELEN and 26101957 to KEY. We receive the scrambled password in the parameter DESTINATION. This scrambled password will be sent to FTP_CONNECT.

Once the connection is established FTP_CONNECT returns the SY-SUBRC = 0. This states that a successful connection on the FTP server through ABAP has been established.

This point on we will pass our commands which we need to perform to the function module FTP_COMMAND. We pass the value of handle we received from the FTP_CONNECT to FTP_COMMAND. Along with this we pass the command to be executed in the parameter COMMAND. The result of execution is returned in an internal table which has a single column of type C with length 132.

After completion of the required functionality we disconnect from the FTP server using the function module FTP_DISCONNECT. Pass the value of handle received from the FTP_CONNECT to FTP_DISCONNECT. This would disconnect from the FTP server. To close the RFC connection, call the function module RFC_CONNECTION_CLOSE. The parameter it accepts is the RFC destination which in our case is SAPFTP.

Example

Following is an example of how to use the FTP server in conjugation with SAP.

The requirement here is that a file is lying on the FTP server which needs to be read and used in SAP. Now our requirement states we need to copy the file from FTP onto the application server and then use it in SAP.

We can also transfer the file from FTP onto the presentation server, but the problem exists if the program is run in background. If run in background then GUI_UPLOAD used to copy the contents from the file into an internal table will fail. So to be on the safe side we will copy the contents on to the application server, so the program works fine in foreground as well as background.

We assume that the working directory on the application server is /usr/sap/tmp.

CALL FUNCTION 'HTTP_SCRAMBLE'
    EXPORTING
      SOURCE      = password
      sourcelen   = lv_len
      key         = lv_key
    IMPORTING
      destination = scrambled_password.

We assume these parameters:

  • password contains the password used to connect to the FTP server.
  • Lv_len is a variable which holds the length of the string password.
  • After execution of this function module the importing parameter contains the scrambled password.

 

It will be passed to the function module to connect to the FTP server.

CALL FUNCTION 'FTP_CONNECT'
    EXPORTING
      user                   = ftp_user
      password               = scrambled_password
*   ACCOUNT                =
      host                   = host
      rfc_destination        = 'SAPFTP'
    IMPORTING
      handle                 = gv_handle
    EXCEPTIONS
      not_connected          = 1
      OTHERS                 = 2.

The parameters are:

  • FTP_USER contains the user name of the FTP server
  • SCRAMBLED_PASSWORD contains the scrambled password received from the previous function module.
  • HOST contains the name of server.
  • GV_HANDLE contains the handle used for further commands.

If the connection is successfully established (SY-SUBRC = 0) then we can go on.

 

We start our operation by first setting the path on the FTP server. Assume the variable lv_path contains the path where the file is stored on the FTP server. Using this path we will build a command and pass it to the function module FTP_COMMAND.

Note that we need to perform a series of commands so we will create a subroutine ftp_command and pass the command in each case.

CONCATENATE 'cd ' lv_path INTO lv_cmd SEPARATED BY space.
PERFORM ftp_command USING lv_cmd.

 

Now we will set the destination path by the following command

CONCATENATE 'lcd' gv_app_ser INTO lv_cmd SEPARATED BY space.
PERFORM ftp_command USING lv_cmd.

Assume that gv_app_ser holds the path on the application server where the file needs to be copied.

 

Now to copy the file from source to the destination the following commands are called

CONCATENATE 'get' lv_filename INTO lv_cmd SEPARATED BY space.
PERFORM ftp_command USING lv_cmd.

Assume lv_filename holds the name of the file that needs to be processed. Using the 'get' command we copy the file from the source (FTP) to the destination (application server).

 

The subroutine ftp_command will contain the following code:

FORM ftp_command  USING lv_cmd.
  DATA mdata ...

  CALL
 FUNCTION 'FTP_COMMAND'
   EXPORTING
     handle                = gv_handle
     command               = lv_cmd
   TABLES
      data                  = mdata[]
   EXCEPTIONS
     tcpip_error           = 1
     command_error         = 2
     data_error            = 3
     OTHERS                = 4.
ENDFORM.

The parameters are:

  • Pass the handle you received during establishing the connection to the above function module,
  • pass the command generated to the COMMAND parameter
  • and receive the results in the TABLES parameter DATA. The internal table mdata is a single column table with the column type C of length 132.

 

Disconnect from the FTP server using this function module:

CALL FUNCTION 'FTP_DISCONNECT'
      EXPORTING
        handle = lv_handle.

 

Use the following function module to close the RFC connection.

CALL FUNCTION 'RFC_CONNECTION_CLOSE'
     EXPORTING
       destination                = 'SAPFTP'
*   TASKNAME                   =
     EXCEPTIONS
       destination_not_open       = 1
       OTHERS                     = 2.


Now we have the file on the application server which can be put into an internal table and processed further as mentioned below:

CONCATENATE gv_app_ser lv_filename INTO gv_app_ser.
OPEN DATASET gv_app_ser FOR INPUT IN TEXT MODE ENCODING DEFAULT WITH SMART LINEFEED.
  IF sy-subrc = 0.
    DO.
      READ DATASET gv_app_ser INTO wa_input.
      IF sy-subrc <> 0.
        EXIT.
      ENDIF.
      APPEND wa_input TO it_input.
    ENDDO.
  ENDIF.

Now the internal table it_input contains the records from the file.

 

Another alternative is to pick the data from the FTP server directly into an internal table in SAP without bringing the presentation or application server into picture.

The same can be achieved by using the function module FTP_SERVER_TO_R3. Here pass the handle, filename on the FTP server and character mode (as X) and receive the contents in the table parameter text. The internal table will be a character type table (one field of type c). Now this table can be used for further processing as per requirement.

Use of the function module FTP_R3_TO_SERVER would copy the contents from an internal table on the FTP server. Before calling this function module we may need to call the FTP_COMMAND and pass a command to change the path to the destination directory on the FTP server.

P.S.  An important point to remember, you will have to check the RFC destination to make sure that the contents are copied to the application server. By default the settings are to copy to the presentation server, if that is not changed then you will receive an error message stating that the defined file or path is not valid as it will try to look for the application server path on the presentation server.

 

One can also go through some standard programs in SAP RSFTP001 to RSFTP009. each of the program will give some insights on how the FTP functionality works. Following are some basic things covered in each of the program

  • RSFTP001 - SAPFTP Version (Current Version one is working on)
  • RSFTP002 - Execute FTP Command
  • RSFTP003 - FTP put / get Test
  • RSFTP004 - FTP Copy
  • RSFTP005 - SAPFTP check
  • RSFTP006 - FTP command list
  • RSFTP007 - Test FB:FTP_SERVER_TO_R3 / FTP_R3_TO_SERVER
  • RSFTP008 - Test FB:FTP_CLIENT_TO_R3 / FTP_R3_TO_CLIENT
  • RSFTP009 - Test FTP put with Verify