Here is afriend's code expanded and tweaked a bit. Still not much success, but I have posted a couple of screenshots following the code.
Read the remark lines in the code for the results I get on various commands. None of this is very repeatable.
Code: Select all
;***************************************************************
;Example by P Dixon ala Afriend
;25 Jan 2008
; Extended and modified TerryHough
;***************************************************************
;
;FTP Commands available - note original text file had "String" or "StringString" or StringStringString"
;appended to some of the commands. I believe these were in error but stand to be corrected:
;
;CloseFTP(#Ftp) - Close the specified client connection And send a notification To the server.
;CreateFTPDirectory(#Ftp ,Directory$) - Create a new directory on the FTP server.
;DeleteFTPDirectory(#Ftp, Directory$) - Delete a directory on the FTP server.
;DeleteFTPFile(#Ftp ,Filename$) - Delete a file on the FTP server.
;ExamineFTPDirectory(#Ftp ,Directory$) - Examine the current directory on the FTP server.
;GetFTPDirectory(#Ftp) - Return the current directory.
;FinishFTPDirectory (#Ftp) - Free the Data allocated With ExamineFTPDirectory.
;FTPDirectoryEntryAttributes (#Ftp) - Return the attributes of the current entry in the last FTP listing.
;FTPDirectoryEntryDate (#Ftp) - Return the modified date of the current entry in the last FTP listing.
;FTPDirectoryEntryName (#Ftp) - Return the name of the current entry in the last FTP listing.
;FTPDirectoryEntrySize (#Ftp) - Return the size of the current entry in the last FTP listing.
;FTPDirectoryEntryType (#Ftp) - Return the type of the current entry in the last FTP listing.
;FTPProgress(#Ftp) - Return the progress on the current transfer.
;IsFTP(#Ftp) - Tests If the given '#Ftp' is an initialized FTP object.
;NextFTPDirectoryEntry(#Ftp) - Go To the Next FTP directory entry
;OpenFTP(#Ftp ,ServerName$, User$, Password$ [Passive [Port]]) - Try To open a connection on the specified server.
;ReceiveFTPFile(#Ftp, RemoteFilename$, Filename$ [Asynchronous]) - Receive a file from a FTP Server.
;RenameFTPFile(#Ftp, Filename, $NewFilename$) - Rename Or move a file on the FTP server.
;SendFTPFile(#Ftp, Filename$, RemoteFilename$ [Asynchronous]) - Send a file To a FTP Server.
;SetFTPDirectory(#Ftp, Directory$) - Change the current directory.
;
;Using the above modified commands works quite well.
;
;
Global Msg$ = ""
Procedure FTP_Results()
; Follow file transfer progress until end or failure
; As near as I can make out FTPProgress(0)
; -1 = in progress transmit file
; -2 = file not found or some error
; -3 = file transmited/received - function complete successful?
; so you could put better traps/filters in but this is just a demo
Repeat
Delay(300)
Result = FTPProgress(0)
Until Result = -3 Or Result = -2
Select Result
Case -1
; Indicates FTP action still in progress
;Msg$ + "Send: Got a -1 result - " + #LF$
Case -2
;Msg$ + "Failed to send/receive file: " + #LF$
ProcedureReturn #False
Case -3
;Msg$ + "Successfully sent/received file: " + #LF$
ProcedureReturn #True
EndSelect
EndProcedure
;Host parameters fictional for exercise - substitute your own legitimate parameters
IPAddress.s = "ftp.server.somewhere"
Username.s = "USERNAME"
Password.s = "PASSWORD"
;First initialize the network
InitNetwork()
;now open ftp
If OpenFTP(0, IPAddress, Username, Password)
; The FTP session has been accepted by the FTP Server
SetFTPDirectory(0,"..")
; Now, create a directory on the server
; CreateFTPDirectory(0,"TESTDIR")
; That worked in initial test
; Now, delete a directory on the server
; DeleteFTPDirectory(0,"TESTDIR")
; That worked in initial test
; Choose and set the remote directory
Directoryname$ = "\RECEIVE" ; a valid directory with permissions on the server
Result = SetFTPDirectory(0, Directoryname$)
; The SetFTPDirectory is received at the server and acted upon.
Delay(300) ; tried various delays up to 5 seconds
Msg$ + "Current remote directory: " + GetFTPDirectory(0) + #LF$
; The GetFTPDirectory is received at the server and replied to, but the current directory name
; is not returned from the command. (See same command later on.)
; Ready to send a file
Filename$ = "NCSec529.pdf" ; valid file stored in the root directory of Drive C:
Msg$ + #LF$ + "Send a file to the server: " + Filename$
SendFTPFile(0, "C:\" + Filename$, Filename$, 1)
If FTP_Results()
Msg$ + " - Successful" + #LF$
Else
Msg$ + " - Failed" + #LF$
EndIf
; I have not been successful sending any file yet. The server never receives any request to store a file.
SetFTPDirectory(0,"\") ; Set back to root directory of server session
Msg$ + #LF$
; Now see if we can examine a directory
Directoryname$ = "\SEND\SENT" ; a valid directory with permissions on the server
SetFTPDirectory(0, Directoryname$) ; change to the desired directory
Delay(300)
Msg$ + "Current remote directory: " + GetFTPDirectory(0) + #LF$
; This GetFTPDirectory works as expected and returns the proper information
ExamineFTPDirectory(0)
Delay(300)
Msg$ + #LF$ + "Examine directory: " + Directoryname$ + #LF$
Msg$ + "--------------------------------------------" + #LF$
While NextFTPDirectoryEntry(0)
Msg$ + FTPDirectoryEntryName(0) + #TAB$
Msg$ + #TAB$ + FormatDate("%mm/%dd;%yyyy %hh:%ii:%ss", FTPDirectoryEntryDate(0)) + #TAB$
Msg$ + RSet(Str(FTPDirectoryEntrySize(0)),10) + " " + #TAB$
Msg$ + Str(FTPDirectoryEntryType(0)) + #LF$
Wend
Msg$ + "--------------------------------------------" + #LF$
FinishFTPDirectory(0)
Msg$ + "Done examining the directory" + #LF$
Delay(300)
Directoryname$ = "\SEND\SENT" ; a valid directory with permissions on the server
;Now get file from host and store on local computer to ensure file transfer OK
SetFTPDirectory(0, Directoryname$) ; change to the desired directory
Delay(300)
Filename$ = "Purebasic.chm" ; Valid file name stored in the requested directory on the server
Msg$ + #LF$ + "Receive file from server: " + Filename$
DeleteFile("C:\" + Filename$) ; delete local copy of file
Delay(300)
ReceiveFTPFile(0, Filename$, "C:\" + Filename$, 1)
If FTP_Results()
Msg$ + " - Successful" + #LF$
Else
Msg$ + " - Failed" + #LF$
EndIf
; The above file is received successfully. But only if the ExamineDirectory routine above
; is not used.
; Reset the directory to root
SetFTPDirectory(0,"\")
Delay(300)
Msg$ + "Current remote directory: " + GetFTPDirectory(0) + #LF$
; Delete a file from the root directory
Filename$ = "testtext"
Msg$ + #LF$ + "Delete a file on the server: " + Filename$
If DeleteFTPFile(0 , Filename$)
Msg$ + " - Successful" + #LF$
Else
Msg$ + " - Failed" + #LF$
EndIf
; An existing file was deleted successfully. If non-existing file, it still returns successful.
; Close the FTP session
CloseFTP(0)
Else
; Network could not be initialized
Msg$ + "Network initialization failed." + #LF$
EndIf
MessageRequester("FTP Library", Msg$, #MB_ICONINFORMATION)
End
Here is the results when the ExamineDirectory works:
And here is an example when the ExamineDirectory code was removed and the ReceiveFTPFile actually worked:
