日期:2011-01-21  浏览次数:20455 次

现在终于到了我们的第三个文件,include.php 它为程序建立起一个用户界面。

"include.php" 包含三个表单,一些PHP代码获取当前的目录列表并将它们存入三个变量
$files (包括当前目录下的文件),
$file_sizes (相应的文件大小),
and $dirs (包含子目录名)

第一个表单使用$dirs 产生一个下拉式目录列表,对应于“action=CWD”。

第二个表单使用$files  $file_sizes创建一个可用的文件列表,每一个文件使用一个checkbox。这个表单的action对应于"action=Delete" and "action=Download"

第三个表单用来上传一个文件到FTP站点,如下:
--------------------------------------------------------------------------------
<form enctype="multipart/form-data" action=actions.php4 method=post>
...
<input type=file name=upfile>
...
</form>
--------------------------------------------------------------------------------
当PHP以这种方式接收到一个文件名,一些变量就产生了,这些变量指定文件的大小,一个临时的文件名以及文件的类型,最初的文件名存在$upfile_name,一旦上传后文件名便存入$upfile中(这个变量是由PHP自己创建的)
通过这些信息,我们就可以创建以下的语句了:
--------------------------------------------------------------------------------
ftp_put($result, $upfile_name, $upfile, FTP_BINARY);
--------------------------------------------------------------------------------
以下是代码列表:
--------------------------------------------------------------------------------
<!-- code for index.html begins here -->
<html>
<head>
<basefont face=arial>
</head>

<body>

<table border=0 align=center>
<form action="actions.php" 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>

</body>
</html>

<!-- code for index.html ends here -->
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
<!-- code for actions.php begins here -->

<html>
<head>
<basefont face=Arial>
</head>
<body>

<?
/*
--------------------------------------------------------------------------------
DISCLAIMER:

This is use-at-your-own-risk code.

It is meant only for illustrative purposes and is not meant for production environments. No warranties of any kind are provided to the user.

You have been warned!

All code copyright Melonfire, 2000. Visit us at http://www.melonfire.com
--------------------------------------------------------------------------------
*/

// function to connect to FTP server
function connect()
{
global $server, $username, $password;
$conn = ftp_connect($server);
ftp_login($conn, $username, $password);
return $conn;
}


// main program begins

// check for valid form entries else print error
if (!$server || !$username || !$password)
{
echo "Form data incomplete!";
}
else
{


// connect
$result = connect();

// action: change directory
if ($action == "CWD")
{

// at initial stage $rdir does not exist
// so assume default directory
if (!$rdir)
{
$path = ".";
}
// get current location $cdir and add it to requested directory $rdir
else
{
$path = $cdir . "/" . $rdir;
}

// change t