Skip to Content

Standard SAP provides loads of function module for doing FTP from an ABAP program. Some of them are:

  • HTTP_SCRAMBLE – To scramble the password
  • FTP_CONNECT – To connect to the remote system
  • FTP_COMMAND – To execute the FTP command
  • FTP_DISCONNECT – To close the FTP connection

Now instead of calling all these function modules individually, we can bundle then in a wrapper function module and call the wrapper function module.

Process Flow:

 For doing FTP the following steps needs to be performed.

1. RFC destination for FTP should be maintained in transaction SM59.

2. Get the host name or ip address of the remote server. Get the user id and password of the RFC user (these can be maintained in a custom Z table).

3. Scramble the password by calling function module ‘HTTP_SCRAMBLE’. This scrambed password needs to be used for making the FTP connection.

4. Establish FTP connection by calling FM ‘FTP_CONNECT’.

5. Issue FTP commands by calling function module ‘FTP_COMMAND’.

6. Finally close the connection by calling function module ‘FTP_DISCONNECT’

Example Code:

Name: Y_FTP

Import Parameters:

 Parameter           Description                   Type        Remarks                
 I_HOST Target system ID Char 40  Mandatory
 I_USER User ID Char 30  Mandatory
 I_PASSWORD Password Char 30  Mandatory
I_RFC_DESTINATION RFC Destination RFCDEST  Mandatory – default value ‘SAPFTP’ 
I_FILE_NAME File name Char 50 Mandatory
I_DIRECTION Direction of transfer Char 1 Mandatory – there can be 2 values: O for outbound & I for inbound
I_LOCAL_FOLDER Full path of local directory String Mandatory
I_REMOTE_FOLDER Full path of remote directory String Mandatory

 

 

 

 

 

 

 

 

 

 

Export Parameters:

 Parameter           Description                   Type        Remarks                
 E_FTP_SUCCESS FTP success flag Char 1  ‘X’ in case FTP successful

 

 

 

Changing and Tables Parameters: None

Exceptions:

Exception    Description 
FTP_CONNECTION_ERROR     Error in FTP Connection     
FTP_ERROR  Error in FTP
LCD_ERROR  Error in executing ‘lcd’
CD_ERROR  Error in executing ‘cd’

 

Source Code:

FUNCTION y_ftp.
*”———————————————————————-
*”*”Local Interface:
*” IMPORTING
*” REFERENCE(I_HOST) TYPE CHAR40
*” REFERENCE(I_USER) TYPE CHAR30
*” REFERENCE(I_PASSWORD) TYPE CHAR30
*” REFERENCE(I_RFC_DESTINATION) TYPE RFCDEST DEFAULT ‘SAPFTP’
*” REFERENCE(I_FILE_NAME) TYPE CHAR50
*” REFERENCE(I_DIRECTION) TYPE CHAR1
*” REFERENCE(I_LOCAL_FOLDER) TYPE STRING
*” REFERENCE(I_REMOTE_FOLDER) TYPE STRING
*” EXPORTING
*” REFERENCE(E_FTP_SUCCESS) TYPE CHAR1
*” EXCEPTIONS
*” FTP_CONNECTION_ERROR
*” FTP_ERROR
*” LCD_ERROR
*” CD_ERROR
*”———————————————————————-

DATA:
l_length TYPE i,”Password length
l_password TYPE c LENGTH 30,
l_key TYPE i VALUE 26101957,
l_ftp_handle TYPE i,”FTP handle
l_cmd TYPE c LENGTH 200,”FTP command
l_error TYPE c.”Error flag

 

* Connect to the FTP server
* Get the length of password
l_length = STRLEN( i_password ).

* Scramble the password
CLEAR: l_password.
CALL FUNCTION ‘HTTP_SCRAMBLE’
EXPORTING
SOURCE = i_password
sourcelen = l_length
key = l_key
IMPORTING
destination = l_password.

* Open connection to the FTP server
CALL FUNCTION ‘FTP_CONNECT’
EXPORTING
user = i_user
password = l_password
host = i_host
rfc_destination = i_rfc_destination
IMPORTING
handle = l_ftp_handle
EXCEPTIONS
not_connected = 1
OTHERS = 2.
IF sy-subrc = 0.

* Change the local folder using lcd command
CLEAR: l_cmd, l_error.
CONCATENATE ‘lcd’ i_local_folder INTO l_cmd SEPARATED BY space.
* Perform the change in local directory
* command is lcd
PERFORM execute_ftp_command USING l_ftp_handle l_cmd
CHANGING l_error.
IF l_error = ‘X’.
* Close the connection
PERFORM close_ftp_connection USING l_ftp_handle.
* Raise LCD_ERROR exception
RAISE lcd_error.
ENDIF.

* Change the remote folder using cd command
CLEAR: l_cmd, l_error.
CONCATENATE ‘cd’ i_remote_folder INTO l_cmd SEPARATED BY space.
* Perform the change in remote directory
* command is cd
PERFORM execute_ftp_command USING l_ftp_handle l_cmd
CHANGING l_error.
IF l_error = ‘X’.
* Close the connection
PERFORM close_ftp_connection USING l_ftp_handle.
* Raise exception
RAISE cd_error.
ENDIF.

* Do the actual file transfer – for outbound it will be put
* For inbound it will be get
CLEAR: l_cmd, l_error.
IF i_direction = ‘O’. “outbound.
CONCATENATE ‘put’ i_file_name INTO l_cmd SEPARATED BY space.
ELSEIF i_direction = ‘I’.”inbound.
CONCATENATE ‘get’ i_file_name INTO l_cmd SEPARATED BY space.
ENDIF.
* Perform the actual File transfer – get or put
* command is the FTP command – get/put
PERFORM execute_ftp_command USING l_ftp_handle l_cmd
CHANGING l_error.

IF l_error = ‘X’.
* Close the connection
PERFORM close_ftp_connection USING l_ftp_handle.
* Raise FTP_ERROR exception
RAISE ftp_error.

ELSE.
* Close the connection
PERFORM close_ftp_connection USING l_ftp_handle.
* FTP is successfull
e_ftp_success = ‘X’.
ENDIF.

ELSE.
* Error in FTP connection
RAISE ftp_connection_error.
ENDIF.

ENDFUNCTION.

*————————————————–*
*————–List of Subroutines —————–*
*&———————————————————————*
*& Form execute_ftp_command
*&———————————————————————*
* This subroutine is used to issue FTP commands
*———————————————————————-*
* –>P_FTP_HANDLE FTP Handle
* –>P_CMND FTP Command
* <–p_error Error flag
*———————————————————————-*
FORM execute_ftp_command USING p_ftp_handle TYPE i
p_cmnd TYPE c
CHANGING p_error TYPE c.

REFRESH: it_result[].
* Issue the FTP command (p_cmnd) by calling this FM
CALL FUNCTION ‘FTP_COMMAND’
EXPORTING
handle = p_ftp_handle
command = p_cmnd
TABLES
data = it_result
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3
OTHERS = 4.

* Error in the FTP command
IF sy-subrc <> 0.
CASE sy-subrc.
WHEN 1.
wa_result-line = ‘TCP/IP communication error'(t30).
WHEN 2.
wa_result-line = ‘FTP command error'(t31).
WHEN 3.
wa_result-line = ‘Internal error in SAP FTP'(t32).
WHEN OTHERS.
wa_result-line = ‘Unknown error in FTP command'(t33).
ENDCASE.
APPEND wa_result TO it_result.
p_error = ‘X’.
ENDIF.
ENDFORM. ” execute_ftp_command

*&———————————————————————*
*& Form close_ftp_connection
*&———————————————————————*
* This subroutine will close an open FTP connection
*———————————————————————-*
* –>P_FTP_HANDLE FTP Handle
*———————————————————————-*
FORM close_ftp_connection USING p_ftp_handle TYPE i.

* Call function FTP_DISCONNECT to close the connection
CALL FUNCTION ‘FTP_DISCONNECT’
EXPORTING
handle = p_ftp_handle.

ENDFORM. ” close_ftp_connection

To report this post you need to login first.

15 Comments

You must be Logged on to comment or reply to a post.

      1. Former Member
        what do you mean by this statement……….”But the whole data channel can be secured”….is it an alternative to SFTP?

        how to do it? please help…even i have same problem..need to SFTP a file to an external server when our firewall is enabled.

        (0) 
  1. Look very good FM, to send files I guess that the tables it_result must contain the records that will be transfer.
    (0) 
      1. Former Member Post author
        Hi it_result is just a place to store the result of FTP commands

        * Table type for FTP result
        TYPES: BEGIN OF ty_result,
                  line(150) TYPE c,
              END OF ty_result.

        * Table for FTP command results
              it_result TYPE STANDARD TABLE OF ty_result,
              wa_result TYPE ty_result,

        (0) 
  2. Former Member
    I have one requirement, I retrived the master data from data base tables, Now the chalenge is sending extracted fiels through the SFTP to other server, Could you please explain me about this SFTP. how to connect and how to transfer the data from sap server to other server

    Please….

    It is very urgent.

    Please guide me

    Thanks,
    kumar

    (0) 
  3. Former Member
    I have one requirement, I retrived the master data from data base tables, Now the chalenge is sending extracted fiels through the SFTP to other server, Could you please explain me about this SFTP. how to connect and how to transfer the data from sap server to other server

    Please….

    It is very urgent.

    Please guide me

    Thanks,
    kumar

    (0) 
    1. Former Member Post author
      Hi,
      SAP FTP functional modules doesn’t support SFTP ๐Ÿ™ . We have also faced the sane issue…one option is to secure the entire ftp data channel. Please put an SDN forum post & see if some better solution is there.
      -Ashim
      (0) 
  4. Hi Ashim,

    thanks for a nice blog. I am using the same functions for one of the requirements. I am able to create a file using FTP in online mode but when I do the same in background mode the function FTP_CONNECT is giving an error. I used RFC SAPFTP for online mode and SAPFTPA for background mode…still I get the same error.

    Whatelse can be different in batch mode. Can you pls help me with some inputs…

    Thanks

    (0) 
  5. Former Member
    Hi,

    May I know how to compress the file before sending to FSTP server?

    I know command -C is used for compression. CanI know how to pass this value in the ABAP code?

    (0) 
  6. Former Member
    Hi Ashim Chowdhury,
    Thank you for your blog.
    It was useful for me.

    Please i have this problem.
    So as to run the FTP_CONNECT FM i have to declare the IP of the client PC where i run my programm in the FTP server.

    Do i have sth to do from abap side ?
    Or is this some FTP Service Configuration?

      Regards,
       Ari

    (0) 
    1. Former Member Post author
      Hi Ari,
      This is the IP of the server to which u wants to connect through FTP. The ABAP program will run in the application server and connect the FTP server from there. The IP address will be passed as a parameter of the function module.
      -Regards
      Ashim
      (0) 
  7. Former Member
    Hi Ashim,

    Is there anyway to use a UNC path to access a network folder using ABAP.

    FTP_CONNECT failed when I used UNC path in the host parameter.

    Is this only for FTP servers? If so, any idea how can we save or retrieve a file from a network folder using ABAP. I tried dsn but it only writes or reads to application server.

    Appreciate your help.

    Thanks,
    Sri

    (0) 

Leave a Reply