By Vikram Vaswani Melonfire November 07, 2000
程序包括以下几个文件: index.html – 登录文件 actions.php –
程序必需的FTP代码 include.php – 程序主界面,它显示文件列表和控制按钮。
table border=0 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 input type=submit value=Beam Me Up, Scotty! /td /tr /form

(包括当前目录下的文件),$file_sizes (相应的文件大小),and $dirs
enctype=”multipart/form-data” action=actions.php4 method=post…input
$upfile_name, $upfile,
code for index.html begins here –htmlheadbasefont
face=arial/headbodytable border=0 align=centerform action=”actions.php”
method=postinput type=hidden name=action value=CWDtrtdServer/tdtdinput
type=text name=server/td/trtrtdUser/tdtdinput type=text
name=username/td/trtrtdPassword/tdtdinput type=password
name=password/td/trtrtd colspan=2 align=centerinput 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 –htmlheadbasefont
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 */// function to connect to FTP
serverfunction 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
errorif (!$server !$username !$password){echo “Form data
incomplete!”;}else{// connect$result = connect();// action: change
directoryif ($action == “CWD”){// at initial stage $rdir does not
exist// so assume default directoryif (!$rdir){$path = “.”;}// get
current location $cdir and add it to requested directory $rdirelse{$path
= $cdir . “/” . $rdir;}// change to requested
directoryftp_chdir($result, $path);}// action: delete file(s)else if
($action == “Delete”){ftp_chdir($result, $cdir);// loop through
selected files and deletefor ($x=0; $xsizeof($dfile);
$x++){ftp_delete($result, $cdir . “/” . $dfile[$x]);}}// action:
download fileselse if ($action == “Download”){ftp_chdir($result,
$cdir);// download selected files// IMPORTANT: you should specify a
different download location here!!for ($x=0; $xsizeof($dfile);
$x++){ftp_get($result, $dfile[$x], $dfile[$x], FTP_BINARY);}}//
action: upload fileelse if ($action == “Upload”){ftp_chdir($result,
$cdir);// put file/*a better idea would be to use$res_code =
FTP_BINARY);as it offers greater security*/$res_code =
ftp_put($result, $upfile_name, $upfile, FTP_BINARY);// check status
and displayif ($res_code == 1){$status = “Upload
successful!”;}else{$status = “Upload error!”;}}// create file
list$filelist = ftp_nlist($result, “.”);// and display
interfaceinclude(“include.php”);// close
connectionftp_quit($result);}?/body/html!– code for actions.php ends
code for include.php begins here –?// get current location$here =
ftp_pwd($result);/*since ftp_size() is quite slow, especially when
workingon an array containing all the files in a directory,this section
performs an ftp_size() on all the files in the currentdirectory and
creates three arrays.*/// array for files$files = Array();// array for
directories$dirs = Array();// array for file sizes$file_sizes =
Array();// counters$file_list_counter = 0;$dir_list_counter = 0;//
check each element of $filelistfor ($x=0; $xsizeof($filelist); $x++){if
(ftp_size($result, $filelist[$x]) != -1){// create
arrays$files[$file_list_counter] =
$filelist[$x];$file_sizes[$file_list_counter] =
= $filelist[$x];$dir_list_counter++;}}?!– header – where am I?
–centerYou are currently working in b? echo $here; ?/bbr!– status
message for upload function –? echo $status; ?/centerhrp!– directory
listing in drop-down list –Available directories:form
action=actions.php method=post!– these values are passed hidden every
time –!– a more optimal solution might be to place these in
sessionvariables –input type=hidden name=username value=? echo
$username; ?input type=hidden name=password value=? echo $password;
?input type=hidden name=server value=? echo $server; ?input type=hidden
name=cdir value=? echo $here; ?!– action to take when THIS form is
submitted –input type=hidden name=action value=CWD!– dir listing
begins; first item is for parent dir –select name=rdiroption
value=”..”parent directory/option?for ($x=0; $xsizeof($dir_list);
$x++){echo “option value=” . $dir_list[$x] . “” . $dir_list[$x] .
“/option”;}?/selectinput type=submit value=Go/formhr!– file listing
begins –Available files:form action=actions.php method=post!– these
values are passed hidden every time –input type=hidden name=server
value=? echo $server; ?input type=hidden name=username value=? echo
$username; ?input type=hidden name=password value=? echo $password;
?input type=hidden name=cdir value=? echo $here; ?table border=0
width=100%?// display file listing with checkboxes and sizesfor ($y=0;
$ysizeof($files); $y++){echo “trtdinput type=checkbox name=dfile[]
value=” . $files[$y] .””. $files[$y] . ” i(” . $file_sizes[$y] .
” bytes)/itd”;}?/table!– actions for this form –centerinput
type=submit name=action value=Deleteinput type=submit name=action
value=Download/center/formphr!– file upload form –File upload:form
enctype=”multipart/form-data” action=actions.php method=post!– these
values are passed hidden every time –input type=hidden name=username
value=? echo $username; ?input type=hidden name=password value=? echo
$password; ?input type=hidden name=server value=? echo $server; ?input
type=hidden name=cdir value=? echo $here; ?tabletrtd!– file selection
box –input type=file name=upfile/td/trtrtd!– action for this form
–input type=submit name=action value=Upload/td/tr/table/form!– code
for include.php ends here —


$username 和 $password
注意那个“hidden” 它传给action.php一个变量$action ,值为CWD。


html head basefont face=Arial /head body !– the include.php interface
will be inserted into this page — ?
if (!$server || !$username || !$password) { echo 提交数据不全!; } else {

// keep reading } ? /body /html

接下来是变量 actions. 程序允许以下的action: action=CWD 改变工作目录
action=Delete 删除指定文件 action=Download 下载指定文件 action=Upload
? // action: 改变目录 if ($action == CWD) { // 具体代码 } // action:
删除文件 else if ($action == Delete) { // 具体代码 } // action: 下载文件
else if ($action == Download) { // 具体代码 } // action: 上传文件 else

if ($action == Upload) { // 具体代码 } ?


通过定制的函数联接并登录FTP服务器 connect(); 转向适当的目录
执行选择的功能 刷新列表,以察看改变的结果

通过include(include.php),显示文件列表和控制按钮 关闭联接

注意: 以下功能支持多文件操作- 即 action=Delete 和 action=Download
它们使用FOR循环来实现。 变量$cdir 和 $here 将在每一阶段实时更新。
现在终于到了我们的第三个文件,include.php 它为程序建立起一个用户界面。
包含三个表单,一些PHP代码获取当前的目录列表并将它们存入三个变量 $files
(包括当前目录下的文件), $file_sizes (相应的文件大小), and $dirs
(包含子目录名) 第一个表单使用$dirs

and action=Download 第三个表单用来上传一个文件到FTP站点,如下:

form enctype=multipart/form-data action=actions.php4 method=post …

input type=file name=upfile … /form



ftp_put($result, $upfile_name, $upfile, FTP_BINARY);

You can leave a response, or trackback from your own site.

Leave a Reply