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 covers the various FTP functions that PHP provides - but, just by itself, it isn't enough. The true power of these functions becomes visible only when they're combined into a single application which provides file transfer and file management capabilities. Since we're talking about FTP functions, it seems obvious that the best application for such a demonstration would be a Web-based FTP client - which is exactly what's coming up next!
Before we get into a code listing, I should make it clear that the example below is meant only to illustrate the various functions you've just learned. While it is functional and can be used as a "real" FTP client, I've omitted many basic error checking routines to make it easier to explain, and you should consider this use-at-your-own-risk software if you decide to implement it in a live environment. And cigarette smoking causes cancer. You have been warned.
The application code is separated across three different files:
index.html - the log in screen
actions.php4 - the code for all FTP transactions
include.php4 - the main interface, which displays file listings and control buttons
A complete code listing (with comments) is available at the end of the article - I'll simply be highlighting the important areas here.
This is the log-in form, with fields for the FTP server name,
the FTP user name and the password; these are stored in the variables $server, $username and $password respectively. Once the form is submitted, it calls the PHP script "actions.php4", which then takes over to initiate the FTP connection.
Note the hidden field, which sends the directive "action=CWD" to "actions.php4" - you'll see what role this plays on the next page.
This article copyright Melonfire 2000. All rights reserved.