Stream Me Up, Scotty! (part 1) - Well-Formed Ideas (
Page 8 of 9 )
Let's now take a quick
look at the third file, "include.php4", which sets up the user interface for the
application.
"include.php4" contains three forms, together with some PHP
code that takes the current list of files and separates it into three
arrays:
$files (whichcontains a list of only the files in the current
directory,
$file_sizes (which contains a list of the corresponding file
sizes),
and $dirs (which contains a list of sub-directories)
The first
of these uses the $dirs array to generate a drop-down list of available
directories, and is linked to "action=CWD".
The second uses the $files
and $file_sizes to create a list of available files, with a checkbox for each.
This form is linked to "action=Delete" and "action=Download", and will send an
array of selected files to "actions.php4" for processing.
You should note
that downloaded files will appear in the directory where these scripts are
running from. Since this directory is sure to be under your Web server root, it
opens up a security hole, and it's therefore essential that you specify another
location for the downloaded files to reside.
The third form is used
specifically to upload a file to the FTP site, and therefore must be of the form
<form enctype="multipart/form-data" action=actions.php4 method=post>
...
<input type=file name=upfile>
...
</form>
When PHP receives a file encoded in this manner, a number of
variables come into existence. These variables specify the size of the file, a
temporary filename and the file type, and will be covered in detail in the
second part of this article. For the moment, all you need to know is that the
original filename is stored in the variable $upfile_name, while the temporary
name of the file, once uploaded, is stored in the variable $upfile (this name is
assigned by PHP).
Using this information, it's possible to construct the
following ftp_put() statement:
ftp_put($result, $upfile_name, $upfile, FTP_BINARY);
Depending on whether or not the upload was successful, a
message is generated and displayed the next time the page loads.
Note
that it's not very difficult to add additional functions to this application -
for example, you could easily add functions to create and delete directories, or
to filter out files of a specific type. And in case you're still stuck with
PHP3, or prefer the HTTP method of file transfer, tune in for the second part of
this article, where I'll be showing you how to perform equivalent file
management tasks with a combination of PHP's file manipulation functions and
HTTP file transfer.
This article copyright Melonfire
2000. All rights reserved.