Stream Me Up, Scotty! (part 1) - Start Me Up! (
Page 6 of 9 )
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.
Let's begin at the top, with "index.html":
<table border=0 align=center>
<form action="actions.php4" method=post>
<input type=hidden name=action value=CWD>
<tr>
<td>
Server
</td>
<td>
<input type=text name=server>
</td>
</tr>
<tr>
<td>
User
</td>
<td>
<input type=text name=username>
</td>
</tr>
<tr>
<td>
Password
</td>
<td>
<input type=password name=password>
</td>
</tr>
<tr>
<td colspan=2 align=center>
<input type="submit" value="Beam Me Up, Scotty!">
</td>
</tr>
</form>
</table>
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.