Using the PHP Function FTP_Connect
FTP is a file transfer protocol used for remote accessing files on a computer. PHP supports a handful of functions that allow you to connect to and handle files on an FTP server. Using these functions you can download, upload and manipulate files. However you will need to make sure that you are logged in to the server and that your connection is set to passive mode before any of this can happen.
To do this you need to use the php function ftp_connect. This is an inbuilt function that creates a new ftp connection to the host or server specified by it. It takes the hostname or IP address of the server and a port number as parameters. It is important to note that these names must not be suffixed with slashes or prefixed with ftp://. It also requires a password to be provided in order to log in to the server. It returns an FTP stream on success and a false value on failure.
When ftp_connect is successful it will return a link identifier for the FTP stream that can be referred to in the other FTP related functions like ftp_get(), ftp_put() and ftp_nlist(). The ftp_nlist() function can be used to display the contents of a directory on the FTP server. A useful function is ftp_chmod(), which can be used to set the permissions of a directory on an FTP server. It is worth noting that starting in PHP 8.1 the is_resource function now returns false instead of true for FTP