FTP

Files on FTP servers: listing directories, downloading, uploading, renaming and deleting.

x has its own FTP client, the same on Windows and Linux. It is compatible with PureBasic 6.50's: the same return values, the same directory names, the same listing and the same progress values.

  • Passive mode is used for transfers (EPSV, or PASV if the server doesn't know EPSV).
  • Where x differs:
    • SFTP ("sftp://…") isn't in x yet: OpenFTP returns 0. SFTP is SSH rather than FTP, so it needs its own client.
    • #PB_FTP_Active is accepted, but transfers still use passive mode, which nearly every server supports. In PureBasic an active connection behind a firewall or router usually fails.
    • #PB_FTP_Debug does nothing.

Connections

OpenFTP ✅

Result = OpenFTP(#Ftp, ServerName$, User$, Password$ [, Flags [, Port]])

Connects and signs in. ServerName$ may start with ftp://. Port is 21 by default. Returns nonzero on success (with #PB_Any: the new connection's number), or 0 if the server can't be reached or doesn't accept the user and password. An empty user and password are sent as given (there's no automatic "anonymous").

The server's current directory is read at once. If the server says it uses UTF-8 (FEAT), names are sent and read as UTF-8.

CloseFTP ✅, IsFtp ✅, CheckFTPConnection ✅

CloseFTP(#Ftp)
Result = IsFtp(#Ftp)
Result = CheckFTPConnection(#Ftp)

CloseFTP says goodbye (QUIT) and closes the connection; #PB_All closes all of them. IsFtp returns nonzero for an open connection (any value is safe to test). CheckFTPConnection returns 0 once the server has closed the connection; it sends nothing to the server.

OpenConsole()
If OpenFTP(0, "127.0.0.1", "anonymous", "", 0, 6899)   ; no server there
  PrintN("connected")
Else
  PrintN("can't connect, IsFtp=" + Str(IsFtp(0)))
EndIf

Output:

can't connect, IsFtp=0

Directories

GetFTPDirectory ✅, SetFTPDirectory ✅

Directory$ = GetFTPDirectory(#Ftp)
Result = SetFTPDirectory(#Ftp, Directory$)

GetFTPDirectory returns the current directory: / at the top, otherwise with a / at the end, as PureBasic does (/pub/). SetFTPDirectory moves into a directory inside the current one, or up with "..". Returns 0 if the server refuses, leaving the current directory as it was.

CreateFTPDirectory ✅, DeleteFTPDirectory ✅

Result = CreateFTPDirectory(#Ftp, Directory$)
Result = DeleteFTPDirectory(#Ftp, Directory$)

In the current directory. A directory must be empty to be deleted. Returns nonzero on success.

ExamineFTPDirectory ✅, NextFTPDirectoryEntry ✅, FinishFTPDirectory ✅

Result = ExamineFTPDirectory(#Ftp)
Result = NextFTPDirectoryEntry(#Ftp)
FinishFTPDirectory(#Ftp)

Lists the current directory. NextFTPDirectoryEntry moves to each entry in turn and returns 0 after the last.

As in PureBasic:

  • The listing is read as Unix servers send it (ls -l), which nearly every server does.
  • Entries come out in the reverse of the server's order.
  • Lines that aren't entries, such as total 42, are skipped.

FTPDirectoryEntryName ✅, FTPDirectoryEntryType ✅, FTPDirectoryEntrySize ✅, FTPDirectoryEntryDate ✅, FTPDirectoryEntryAttributes ✅, FTPDirectoryEntryRaw ✅

Name$ = FTPDirectoryEntryName(#Ftp)
Type = FTPDirectoryEntryType(#Ftp)
Size.q = FTPDirectoryEntrySize(#Ftp)
Date = FTPDirectoryEntryDate(#Ftp)
Attributes = FTPDirectoryEntryAttributes(#Ftp)
Line$ = FTPDirectoryEntryRaw(#Ftp)

The current entry:

  • Type: #PB_FTP_File (1) or #PB_FTP_Directory (2). A link counts as a file, and its name is the server's name -> target.
  • Date: a Date value. A date the server gives without a year (recent files) gets the current year.
  • Attributes: the Unix permissions, from #PB_FTP_ReadUser (1024), WriteUser (512) and ExecuteUser (256), through ReadGroup (64), WriteGroup (32) and ExecuteGroup (16), to ReadAll (4), WriteAll (2) and ExecuteAll (1). So rwxr-x--- is $750.
  • Raw: the server's whole line, its bytes as characters 0-255, for listings x can't read.
If ExamineFTPDirectory(0)
  While NextFTPDirectoryEntry(0)
    If FTPDirectoryEntryType(0) = #PB_FTP_Directory
      PrintN("[" + FTPDirectoryEntryName(0) + "]")
    Else
      PrintN(FTPDirectoryEntryName(0) + "  " + Str(FTPDirectoryEntrySize(0)) + " bytes")
    EndIf
  Wend
  FinishFTPDirectory(0)
EndIf

Files

ReceiveFTPFile ✅, SendFTPFile ✅

Result = ReceiveFTPFile(#Ftp, RemoteFilename$, Filename$ [, Asynchronous])
Result = SendFTPFile(#Ftp, Filename$, RemoteFilename$ [, Asynchronous])

Downloads a file from the current directory, or uploads one into it. Returns nonzero once it's done, or 0 on failure. With Asynchronous set to #True, these return at once (nonzero if the transfer could be set up) and the transfer runs as FTPProgress is called. Only one transfer per connection at a time.

As in PureBasic, a download of a file the server doesn't have still leaves an empty local file behind.

FTPProgress ✅, AbortFTPFile ✅

Result.q = FTPProgress(#Ftp)
AbortFTPFile(#Ftp)

Moves an asynchronous transfer on. Returns the bytes moved so far, or:

  • #PB_FTP_Started (-1): the first call, which sets the transfer up;
  • #PB_FTP_Finished (-3): done;
  • #PB_FTP_Error (-2): failed, or aborted with AbortFTPFile.
If ReceiveFTPFile(0, "big.zip", "big.zip", #True)
  Repeat
    Progress.q = FTPProgress(0)
    If Progress > 0 : PrintN(Str(Progress) + " bytes") : EndIf
    Delay(10)
  Until Progress = #PB_FTP_Finished Or Progress = #PB_FTP_Error
EndIf

DeleteFTPFile ✅, RenameFTPFile ✅

Result = DeleteFTPFile(#Ftp, Filename$)
Result = RenameFTPFile(#Ftp, Filename$, NewFilename$)

In the current directory. Return nonzero on success.

eXperimental is an independent implementation: no proprietary PureBasic source code or implementation code is incorporated into it. PureBasic is a product of Fantaisie Software. eXperimental is neither affiliated with nor endorsed by Fantaisie Software.