Building An FTP Client With PHP - Retrieving a File Listing (Page 3 of 7 )
The ftp_nlist function returns a list of files in a given directory. We can use this function in combination with ftp_pwd to retrieve a list of files and directories from the Microsoft FTP web site. Its signature looks like this:
All of the values returned from ftp_nlist were directories in this case. One of the great features of the ftp_nlist function is that we can use a wildcard value for its second parameter, like this:
$fList = ftp_nlist($conn, "/p*");
This would return all files and directories in the root directory that started with the letter "p". It's great to list the files in a directory, but we also need to be able to change into other directories to traverse the remote FTP server. We can do this in PHP by using the ftp_chdir function, whose signature looks like this:
The first parameter is the connection resource and the second is the name of the directory that we wish to change into. We can change into the first available directory and list its files with this snippet of code:
if(is_array($fList1)) { echo "Files for directory: $newDir<br><br>";
for($i = 0; $i < sizeof($fList1); $i++) { echo $fList1[$i] . "<br>"; } } else { echo "$newDir contains no files."; } } else { echo "An error occured while changing to $newDir or $newDir is not a directory"; } } else { echo "$workingDir contains no files."; }
If the code shown above looks a little complicated, allow me to explain it. First off, we use the ftp_nlist command to retrieve a list of directories and files from the FTP server. We then attempt to change into that directory with ftp_chdir. If this fails, then we've either tried to treat a file as a directory, or an error occurred on the server.
Next, we use the ftp_rawlist function to retrieve the files and directories from our new directory and list then using a for loop, exactly the same way we have in previous examples. The only difference between ftp_rawlist and ftp_nlist is that ftp_rawlist also contains file permissions, owners, and the date that each file or directory was last modified.
The output from the above script looked like this on my PC:
Files for directory: /bussys
dr-xr-xr-x 1 owner group 0 Nov 30 2000 1394 dr-xr-xr-x 1 owner group 0 Jun 6 2000 backoffice dr-xr-xr-x 1 owner group 0 Mar 1 2000 clients dr-xr-xr-x 1 owner group 0 Jan 26 2001 distapps dr-xr-xr-x 1 owner group 0 Feb 24 2000 exchange dr-xr-xr-x 1 owner group 0 Jul 2 12:14 gen_info dr-xr-xr-x 1 owner group 0 Feb 24 2000 iis dr-xr-xr-x 1 owner group 0 Jul 2 12:14 lanman dr-xr-xr-x 1 owner group 0 Jul 2 12:13 mail dr-xr-xr-x 1 owner group 0 Feb 24 2000 mcis dr-xr-xr-x 1 owner group 0 Oct 5 2000 mspress -r-xr-xr-x 1 owner group 1715 May 20 1996 readme.txt -r-xr-xr-x 1 owner group 2107 Jul 2 11:25 readme1.txt dr-xr-xr-x 1 owner group 0 Feb 24 2000 sitesrv dr-xr-xr-x 1 owner group 0 Jul 2 12:46 sql dr-xr-xr-x 1 owner group 0 Mar 14 2000 sysmgrsrv dr-xr-xr-x 1 owner group 0 Feb 24 2000 utilities dr-xr-xr-x 1 owner group 0 Feb 24 2000 viper dr-xr-xr-x 1 owner group 0 Jul 2 12:45 winnt dr-xr-xr-x 1 owner group 0 Feb 25 2000 winsock