PHP Functions For FTP
FTP is one of the most common ways for servers to make files publicly accessible for login and download. PHP makes it easy to use FTP with a set of FTP functions such as ftp_connect(), ftp_put() and ftp_nlist() to create, read and view directories. However, it can be a little complicated to understand how these function work together to handle files on an FTP server.
In order to transfer files to and from an FTP server you must first log into the server. To do this you must connect to the FTP server using the ftp_connect() function. Once connected you need to have a username and password to access the server. The ftp_login() function takes these credentials and allows you to move around the file system on the server.
You can also upload and download files using the ftp_put() and /ftp_get() functions. These functions take a local open file handler as their first parameter and a remote file on the FTP server as the second. Both of these functions have a final optional parameter which controls how the transferred file is handled. The default is FTP_ASCII which transfers the file as plain text (with linefeed conversions). If you have a binary file on the FTP server you can change this parameter to FTP_BINARY which will transfer the file in a binary format.
If you need to get a list of files on an FTP server you can use the ftp_nlist() function or the more advanced ftp_rawlist(). ftp_nlist() will return a list of files on the given directory including information such as permissions, user, group, size(in bits), last modified date(month, day, time) and name.