#native_company# #native_desc#
#native_cta#

Implementing Internet Protocols with PHP Page 2

By Leidago Noabeb
on January 12, 2011

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.