PHP4 comes with a bunch of new functions designed to allow FTPconnections over the Web. This article explores the new FTP functions,together with an example of how they can be used to build a browser-basedFTP client.
Once logged in to an FTP server, PHP offers a number of functions that can provide system-specific information, together with file and directory information.
First, the ftp_pwd() function comes in very useful if you need to find out where you're currently located in the directory tree.
<?
// get current location
$here = ftp_pwd($conn);
?>
In case you need to know the operating system that the FTP
server is currently running, ftp_systype() will be able to give you this information.
<?
// get system type
$server_os = ftp_systype($conn);
?>
In case you need to switch passive (PASV) mode on or off, PHP
has the ftp_pasv() function, which acts as a toggle switch, turning PASV mode on and off. In case you don't know what passive mode...don't worry about it!
<?
// turn PASV on
ftp_pasv($conn, 1);
?>
How about a file listing? Well, PHP offers two types of
listings - a compact listing containing only file and directory names, and a long listing containing detailed information on file sizes, permissions and timestamps.
The first listing is provided by the ftp_nlist() function, while the second comes via ftp_rawlist(). Both functions require a directory name to be passed as a parameter, and both return the listing as an array. Each element of this array corresponds to one line of text from the listing.
And in case you're curious about file sizes, there's a very
handy ftp_size() function which returns the size of the specified file in bytes. An important point to be noted here is that this function returns a value of -1 on error - a fact which comes in handy if you're trying to distinguish between files and directories, since this function, when invoked on a directory, typically returns a value of -1. You'll see this technique being used extensively in the example coming up later.