Stream Me Up, Scotty! (part 1) - GETting It Right (
Page 5 of 9 )
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"
ftp_chdir($conn, "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
ftp_cdup($conn);
?>
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"
ftp_mkdir($conn, "test");
// remove the directory "test"
ftp_rmdir($conn, "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:
<?
// upload
ftp_put($conn, "xyz.txt", "abc.txt", FTP_ASCII);
?>
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
<?
// download
ftp_get($conn, "hers.zip", "his.zip", FTP_BINARY);
?>
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.