In order to make transactions with FTP in ABAP, the server you will use must be in the allowed list in the SAPFTP_SERVERS table, otherwise you cannot connect to the server.
Let’s do our data definitions first
DATA: LV_USER(30) TYPE C VALUE 'FTPUSERNAME',"FTP sunucusu kullanıcı adı LV_PWD(30) TYPE C VALUE 'FTPSIFRE', "FTP Sunucusu şifresi LV_HOST(64) TYPE C VALUE 'FTPADRES', "FTP Adresi LV_DEST LIKE RFCDES-RFCDEST VALUE 'SAPFTP'."RFC Hedefi
As can be understood from the variable names here, LV_USER is the username, LV_PWD password, and LV_HOST is the ip address of the ftp server. The LV_DEST variable sets whether the process runs in the foreground or background. If you are using SAPFTP now, you need to type SAPFTPA if you want it to run in the background.
Second, we have callback variables
DATA: LV_HDL TYPE I, LV_KEY TYPE I VALUE 26101957,"FTP şifresi hash key rastgele numara LV_SLEN TYPE I.
LV_HANDLE is the callback variable of the FTP process, if it returns 1, it means there is a problem with the connection on the server. I will talk about LV_KEY a little later, LV_SLEN is the length of ftp password.
DATA: LV_FILE TYPE CHAR200 VALUE ‘deneme.txt’.
I used the LV_FILE variable as the name of the file on the FTP server.
LV_SLEN = STRLEN( LV_PWD ).
First, let’s fill in our LV_SLEN variable. Then, SAP uses a password scrambling function module so that the ftp password cannot be read outside the SAP system.
CALL FUNCTION 'HTTP_SCRAMBLE' EXPORTING SOURCE = LV_PWD SOURCELEN = LV_SLEN KEY = LV_KEY IMPORTING DESTINATION = LV_PWD.
Here LV_PWD is our ftp password, and LV_KEY is a random hashkey used in the password hash process. For the exit parameter destination, we give the variable LV_PWD again.
Now we connect to FTP Server with FTP_CONNECT function module.
CALL FUNCTION 'FTP_CONNECT' EXPORTING USER = LV_USER PASSWORD = LV_PWD HOST = LV_HOST RFC_DESTINATION = LV_DEST IMPORTING HANDLE = LV_HDL EXCEPTIONS NOT_CONNECTED = 1 OTHERS = 2.
If the connection is successful, sy-subrc should return 0.
I brought the first 100 lines of MARA for the file to be written to FTP. You can export another table instead.
DATA: GT_MARA LIKE TABLE OF MARA. SELECT * FROM MARA INTO TABLE GT_MARA UP TO 100 ROWS.
Using the FTP_R3_TO_SERVER function module, we write the table in plain text to a txt file and upload this file to the ftp server.
CALL FUNCTION 'FTP_R3_TO_SERVER' EXPORTING HANDLE = LV_HDL FNAME = LV_FILE CHARACTER_MODE = 'X' TABLES TEXT = GT_MARA EXCEPTIONS TCPIP_ERROR = 1 COMMAND_ERROR = 2 DATA_ERROR = 3 OTHERS = 4.
The CHARACTER_MODE here writes internal table into a C type single field .
The opposite of this function is ‘FTP_SERVER_TO_R3’. The parameters are exactly the same.
After we are done, we need to disconnect ftp and then rfc.
You can do them by calling the function modules below.
CALL FUNCTION 'FTP_DISCONNECT' EXPORTING HANDLE = LV_HDL. CALL FUNCTION 'RFC_CONNECTION_CLOSE' EXPORTING DESTINATION = LV_DEST EXCEPTIONS OTHERS = 1
There is an extra FTP_COMMAND function module, which allows us to use unix commands in the FTP standard.
You can access the ordered list of commands at
https://www.serv-u.com/linux-ftp-server/commands
My server’s OS is CentOS 6 (i686), commands may differ.
CALL FUNCTION 'FTP_COMMAND' EXPORTING HANDLE = LV_HDL COMMAND = 'ls' TABLES DATA = LV_STR[] EXCEPTIONS TCPIP_ERROR = 1 COMMAND_ERROR = 2 DATA_ERROR = 3 OTHERS = 4.
You can view the files you create with ftp programs such as Filezilla and WinSCP.