Home arrow PHP arrow Page 3 - Building An FTP Client With PHP
PHP

Building An FTP Client With PHP


Ever wondered how to make your own FTP client with PHP? Look no further. In this article Mitchell teaches you everything you need to do just that.

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 71
July 02, 2002
TABLE OF CONTENTS:
  1. · Building An FTP Client With PHP
  2. · PHP and FTP 101
  3. · Retrieving a File Listing
  4. · Retrieving a File
  5. · Building an FTP App
  6. · The GetFile function
  7. · Conclusion

print this article
SEARCH DEVARTICLES

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:

array ftp_nlist ( resource ftp_stream, string directory)

We can return a list of files and directories with the following extract of code:

<?php

$ftpServer = "ftp.microsoft.com";
$ftpUser = "anonymous";
$ftpPass = "me@myhost.com";

set_time_limit(160);

$conn = @ftp_connect($ftpServer)
or die("Couldn't connect to FTP server");

$login = @ftp_login($conn, $ftpUser, $ftpPass)
or die("Login credentials were rejected");

$workingDir = ftp_pwd($conn);

echo "Files for directory: $workingDir<br><br>";

$fList = @ftp_nlist($conn, $workingDir);

if(is_array($fList))
{
for($i = 0; $i < sizeof($fList); $i++)
{
echo $fList[$i] . "<br>";
}
}
else
{
echo "$workingDir contains no files.";
}


ftp_quit($conn);

?>


The output from the code above looks like this:

Files for directory: /

/bussys
/deskapps
/developr
/kbhelp
/misc
/misc1
/peropsys
/products
/reskit
/services
/softlib

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:

bool ftp_chdir ( resource ftp_stream, string directory)

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:

$workingDir = ftp_pwd($conn);
$fList = ftp_nlist($conn, $workingDir);

if(is_array($fList))
{
$newDir = $fList[0];

if(@ftp_chdir($conn, $newDir))
{
$fList1 = ftp_rawlist($conn, $newDir);

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
blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials