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 what is called 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 RFC's or Request for Comments. These protocols are defined by the Internet Engineering Task Force (IETF). And can be found at rfc-editor.org.
Because 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 $fh is the file handle of the open file.

ftp_fput($connect,"new.txt",$fh,FTP_ASCII)

d.txt 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 list all the FTP functions that PHP offers, but it shows the most commonly used functions.

The FTP Application

The application that we are going to create is going to implement the FTP protocol as fully as possible using the above listed functions. The main purpose of a FTP client is to enable file transfer on a network. This includes downloading, uploading, removing and traversing the directory structure of a drive. Our application will do all of this. Below is a listing of the cascading style that I designed for the FTP client application:
/* CSS Document */
body {
        background-color: #FFFFFF;
        margin:0px;
        font-family: Verdana, Arial, Helvetica, sans-serif;
        font-size: 24px;
}
a {
        color: #cc0000;
        text-decoration:none;
        font-weight:bold;
        }
a:hover {
        color: #990000;
        text-decoration:underline;
        }
h1 {
        color: #006699;
        font-size: 14px;
        font-weight: bold;
        margin: 0px 0px 5px 0px;
        border-bottom: 2px solid #006699;
}
h2 {
        color: #006699;
        font-size: 13px;
        font-weight: bold;
        margin: 0px 0px 0px 0px;
}
td {
        font-size: 12px;
        color: #333333;
}
.bar_central {
        background-image:  url("img/bg_central.png");
        height: 28px;
        font-size: 12px;
        color: #FFFFFF;
        padding-bottom: 3px;
        background-repeat: repeat-x;
}
.bar_central a {
        color: #FFFFFF;
        font-size: 12px;
        text-decoration: none;
}
.bar_central a:hover {
        text-decoration: underline;
}
.navhead_text {
        font-size: 12px;
        color: #FFFFFF;
        padding-bottom: 2px;
        padding-left: 10px;
        background-repeat: repeat-x;
}
.navblock {
        border-left: 1px solid #006699;
        border-right: 1px solid #006699;
        border-bottom: 1px solid #CCCCCC;
        background-image: url("img/bg_nav.png");
        background-repeat: repeat-y;
        padding-left: 5px;
        height: 23px;
        color: #006699;
        text-align: left;
}
.navblock a {
        text-decoration: none;
        color: #006699;
}
.navblock a:hover {
        text-decoration: none;
        color: #FE9900;
}
.tooltip {
        color: #006699;
        font-style: italic;
        font-size: 10px;
}
.tooltipblack {
        color: #333333;
        font-style: italic;
        font-size: 10px;
}
.navholder {
        padding-left: 8px;
        padding-top: 20px;
}
.contentholder {
        padding-top: 20px;
}
.bottom {
        border-top: 1px solid #999999;
        padding-top: 10px;
        color: #ccc;
}
.subbox {
        background-color: #F0F0f0;
        border-left: 1px solid #CCCCCC;
        padding-left: 7px;
        padding-right: 7px;
        padding-top: 4px;
        border-right: 1px solid #CCCCCC;
}
#mainarea {
        margin-top: 15px;
}
.dottedline {
        background-image:  url("img/bg_horizdots.gif");
        background-repeat: repeat-x;
        padding: 0px;
        height: 1px;
}
.input100 {
        border: 1px solid #999999;
        font-family: Tahoma, Arial, Verdana;
        font-size: 11px;
        color: #333333;
        background-color: #FFFFFF;
        padding-left: 4px;
        padding-right: 4px;
        width: 100px;
        height: 20px;
}
.input200 {
        border: 1px solid #999999;
        font-family: Tahoma, Arial, Verdana;
        font-size: 11px;
        color: #333333;
        background-color: #FFFFFF;
        padding-left: 4px;
        padding-right: 4px;
        width: 200px;
        height: 20px;
}
.input300 {
        border: 1px solid #999999;
        font-family: Tahoma, Arial, Verdana;
        font-size: 11px;
        color: #333333;
        background-color: #FFFFFF;
        padding-left: 4px;
        padding-right: 4px;
        width: 300px;
        height: 20px;
}
.textarea200 {
        border: 1px solid #999999;
        font-family: Tahoma, Arial, Verdana;
        font-size: 11px;
        color: #333333;
        background-color: #FFFFFF;
        padding: 4px;
        width: 200px;
        height: 125px;
}
.textarea300 {
        border: 1px solid #999999;
        font-family: Tahoma, Arial, Verdana;
        font-size: 11px;
        color: #333333;
        background-color: #FFFFFF;
        padding: 4px;
        width: 300px;
        height: 250px;
}
.tableheader1 {
        background-color: #006699;
        color: #FFFFFF;
        font-family: Tahoma, Arial, Verdana;
        font-weight: bold;
        font-size: 13px;
}
.tableshading1a {
        background-color: #B1D3ED;
}
.tableshading1b {
        background-color: #D0E2F2;
}
.tableheader2 {
        background-color: #CC0000;
        color: #FFFFFF;
        font-family: Tahoma, Arial, Verdana;
        font-weight: bold;
        font-size: 13px;
}
.tableshading2a {
        background-color: #D59299;
}
.tableshading2b {
        background-color: #DBB5BC;
}
.tableheader3 {
        background-color: #555555;
        color: #FFFFFF;
        font-family: Tahoma, Arial, Verdana;
        font-weight: bold;
        font-size: 13px;
}
.tableshading3a {
        background-color: #DDDDDD;
}
.tableshading3b {
        background-color: #EEEEEE;
}
.headertxt{
font-size:24px;
color:#FFFFFF;
background-color:#669999;
}
.copyright{
font-family:Tahoma, Arial, Verdana;
text-align:right;
font-size:11px;}
.smallheader{
background-color:#669999;
font-weight:bold;}
.tdr{
background-color:#a4c2c2;}
.logo {
        font:19px times new roman;
        color: #333300;
        letter-spacing:.4em;
        font-weight:bold;
        }
/**divs*/
.block{
        background-color:#CCCCCC;
        border: 1px solid #ccc;
        left: 659px;
        top: 7px;
        height: 10px;
        width:80px;
overflow:scroll;
}
Copy and paste this code into a new document and save it as ftp.css. Don't worry if you don't understand the code, it is basically just styles that I've put together to make the presentation of the FTP user interface easier to read and understand.

Listfiles Function Explained

Next, we define a commonly used function. The function is contained in a file called functions.php. It defines a function called listfiles(). This function takes two arguments, the connection resource and the directory:
<?php
//list files
function listfiles($con,$directory){
echo '<table>';
echo '<tr>
    <td></td>
    <td>'.ftp_pwd($con).'</td>
  </tr>
  <tr>
    <td><b>File Name</b></td>
    <td><b>Action</b></td>
  </tr>';
   $a = ftp_nlist($con, $directory);
                foreach($a as $value){
                 if(strpos($value,'.') > 0){
                         $img='images/subpage.gif';
                         $open="";
                         }else{
                                 $img='images/fb.gif';
                                 $open='<a href="'.$_SERVER['PHP_SELF'].'?action=view&dirs='.$value.'">
<img src="images/open.png" border="0">Open</a>'; } echo '<tr class="block">'; if($open==""){ echo '<td><img src="'.$img.'" border="0">'.trim($value).' </td> <td><a href="'.$_SERVER['PHP_SELF'].'?action=upload&file='.$value.'"><img src="images/upload.bmp" border="0">
</a>|<a href="'.$_SERVER['PHP_SELF'].'?action=download&file='.$value.'">
<img src="images/download.png" border="0"></a>|<a href="'.$_SERVER['PHP_SELF'].'?action=delete&file='.$value.'">
<img src="images/trash.png" border="0"></a> </td>'; }else{ echo '<td><img src="'.$img.'" border="0">'.trim($value).'</td><td> '.$open.''; echo '</td>'; } echo '</tr>'; } // close the connection //ftp_close($con); echo '</table>'; } ?>
The function starts by building a dynamic table with the headers File and Action:
echo '<table>';
echo '<tr>
    <td></td>
    <td>'.ftp_pwd($con).'</td>
  </tr>
  <tr>
    <td><b>File Name</b></td>
    <td><b>Action</b></td>
  </tr>';
Then it calls the ftp_nlist() function to list the files in the given directory and stores the result in a variable called $a:
   $a = ftp_nlist($con, $directory);
Now remember that the file names are returned in an array, so we must break the array down to list the individual files. The foreach loop does exactly that:
foreach($a as $value){
Sometimes a directory name is returned instead of a file name, so we need to be able to separate the two. The code below makes that separation by checking if the returned name contains a dot (.). This is because all file names contain a dot that separate the actual file name from the extension:
if(strpos($value,'.') > 0){
                         $img='images/subpage.gif';
                         $open="";
If there is no dot then the code proceeds to display the appropriate image and directory name. In addition it also creates a link that enables users to access or open up the directory to view its contents:

                         }else{
                                 $img='images/fb.gif';
                                 $open='<a href="'.$_SERVER['PHP_SELF'].'?action=view&dirs='.$value.'">
<img src="images/open.png" border="0">Open</a>'; }
The remaining code simply sets up three other links that enable the user to upload, download and delete a file from the list or directory. If the listed name is that of a directory than only one link is displayed:
echo '<tr class="block">';
    if($open==""){
 echo '<td><img src="'.$img.'" border="0">'.trim($value).'
    </td>
    <td><a href="'.$_SERVER['PHP_SELF'].'?action=upload&file='.$value.'"><img src="images/upload.bmp" border="0">
</a>|<a href="'.$_SERVER['PHP_SELF'].'?action=download&file='.$value.'">
<img src="images/download.png" border="0"></a>|<a href="'.$_SERVER['PHP_SELF'].'?action=delete&file='.$value.'">
<img src="images/trash.png" border="0"></a> </td>'; }else{ echo '<td><img src="'.$img.'" border="0">'.trim($value).'</td><td> '.$open.''; echo '</td>'; } echo '</tr>'; }
The table is then closed and the connection to the FTP server is also closed. Figure 1 shows an image of what this function accomplishes:


Click here for larger image

Figure 1. FTP::Control Panel Showing listfiles in Action
Remember to activate the FTP server before running this FTP client. The password and usernames that I've used here are located in a file called ftpconnect.php. If you are going to store the connection details in this way, make sure that this file is stored outside of your root directory.