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.
Now that you know where you are and who's around you, it's time to start moving around in the directory tree - and the function that lets you do that is ftp_chdir(), which accepts a directory name as parameter.
// change directory to "public_html"
If all you want to do is go up to the parent directory, an
alternative way of accomplishing this is the ftp_cdup() command, which transports you one level up from wherever you are currently.
// go up one level in the directory tree
You can also create and remove directories with ftp_mkdir()
and ftp_rmdir(); note that ftp_mkdir() returns the name of the new directory if successful.
// make the directory "test"
// remove the directory "test"
The purpose of opening an FTP session is usually to transfer
files - so let's get to that!
Uploading a file is accomplished by means of the ftp_put() command, which requires you to specify a name for the file on the remote system, the name of the file to be uploaded, and the type of transfer. For example, if you'd like to upload the file "abc.txt" and rename it to "xyz.txt" on the remote system, the command would look something like this:
Alternatively, if you'd like to download a file, the built-in
function is ftp_get(), which also needs both remote and local file names, although the order is reversed. If the file to be downloaded is named "his.zip" on the remote system and you plan to save it as "hers.zip" on the local system, the syntax would change to
PHP defines two modes for file transfer - FTP_BINARY and
FTP_ASCII - which need to be specified as they are displayed here, without quotes. Note that both ftp_get() and ftp_put() return result codes, indicating whether or not the transfer was successful.
This article copyright Melonfire 2000. All rights reserved.