Implementing Internet Protocols with PHP
(Page 1 of 4 )
PHP has many functions that help us to implement Internet and/or networking protocols. In this article, we will look at how to implement some of those protocols using PHP.
What are protocols?
Protocols are like rules of communication for a given situation. For example, when you meet someone at night, you would have a conversation that goes something like this:
“Good evening, how are you doing?”
Now, you say, “good evening,” because that is the accepted rule for greeting someone at that time of the day. That is a protocol. Computer or Internet protocols work in the same way. For example, to transfer a file, you use the File Transfer Protocol, to send and receive web pages, we use HTTP or Hyper Text Transfer Protocol, and so on. Most of the protocols are described in documents called RFCs, or Request for Comments. These protocols are defined by the Internet Engineering Task Force (IETF) and can be found at http://www.rfc-editor.org.
Since there are hundreds of protocols, we are not going to write about all of them. Instead, we are going to look at the most popular ones. To start with, we are going to create an FTP client that will carry out all of the tasks that a normal FTP client application normally carries out. A useful FTP client will enable you to do the following:
And this is what our FTP client application will offer us. So what functions does PHP offer us in this regard? Take a look at the list below:
Function | Description |
ftp_cdup($connect) | Changes to the directory directly above the current directory. |
ftp_chdir($connect, ” directoryname ”)
| Changes directories on the remote computer. |
ftp_close($connect) | Closes an FTP connection. |
ftp_connect(“ servername ”) | Opens a connection to the computer (servername can be a domain name or an IP address). |
ftp_delete($connect, path/filename ”) | Deletes a file on the remote computer. |
ftp_exec($connect, ” command ”) | Executes a system command on the remote computer. |
ftp_fget($connect,$fh, ”data.txt”,FTP_ASCII) | Downloads the file contents from the remote computer ($fh is the file handle of the open file). |
ftp_fput($connect ,”new.txt”,$fh,FTP_ASCII) | $fh is the file handle of the open file. |
ftp_get($connect,”d.txt”, ”sr.txt”,FTP_ASCII) | Downloads a file from the remote computer (sr.txt is the name of the file to be downloaded, and d.txt is the name of the downloaded file). |
ftp_login($connect, $userID,$password) | Logs into the FTP server. |
ftp_mdtm($connect, ”filename.txt”) | Gets the time when the file was last modified. |
ftp_mkdir($connect, ” directoryname ”) | Creates a new directory on the remote computer. |
ftp_nlist($connect , ” directoryname ”) | Gets a list of the files in a remote directory. Files are returned in an array. |
ftp_put($connect,”d.txt”, ”sr.txt”,FTP_ASCII) | Uploads a file to the remote computer (sr.txt is the name of the file to be uploaded, and d.txt is the filename on the remote computer). |
ftp_pwd($connect) | Gets the name of the current directory on the remote computer. |
The above list does not show all the FTP functions that PHP offers, but it shows the most commonly used functions.
Next: The FTP Application >>
More PHP Articles
More By David Web