Implementing Internet Protocols with PHP - Listfiles Function Explained (
Page 3 of 4 )
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. Please, take a look:
<?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. It then stores the result in a variable called $a:
$a = ftp_nlist($con, $directory);