<? php 
//设置ftp链接使用的变量
//ftp服务器地址
$ ftp_server='ftp地址';
//ftp服务器地址
$ ftp_user='ftp用户名';
$ ftp_pass='ftp密码';
//检查$_get["action"](
if(isset($_get["action"]))
{
$action=$_GET["action"];

}
else 

$action="showform";
}
//根据变量调用相关代码或函数
switch($action)
{
case "showFile":
ShowFile(); //显示选择文件窗口
break;
case "showForm" :
    break;
case "upload":
//链接ftp服务器
//使用ftp_connect()函数取得ftp链接句柄,如果失败,显示错误信息
$ftp-ftp_connect($ftp_server) or die("链接ftp服务器失败!";
//ftp_login()函数登陆服务器
$ftp_result=ftp_login($ftp,$ftp_user,$ftp_pass);
//检查登陆状态,如果ftp_connect(和ftp_login()函数返回的是false值,显示链接失败信息
if((!$ftp)||(! $ftp_result))
{
echo '链接ftp服务器失败!请检查ftp相关参数<br>';

exit();
}else {
      echo 'ftp登陆成功!<br>';
}
foreach($_post["ftp"] as $key=>$file)
{
if($file=="")
{
continue;
}else 
{
$source_file=$file;  //设置上传目录
$destination_file=$file; //设置目标文件
}
//设置远程存储文件的目录和文件名r
//使用ftp_put()函数上传文件到目标文件夹
$upload=ftp_put($ftp,$destination_file,$source_file,FTP_BINARY) or die("链接ftp服务器失败!");
//检查上传状态
if($upload==false)
{
echo '文件上传失败!<br>';

}
else 
{
echo '源文件上传成功!<br>';

}
}
//关闭ftp_链接
ftp_close($ftp);
break;
}
function showForm()
{
? >
<form action="?action=upload" method="POST" name="sendfile">
<div id="filehtml">
选择文件上传1:<input name="ftp[1]" type="text" id-"ftp[1]" />
<input bype="button" name="submit1" value="浏览"  
onclick="window.open('5_3php?action=showFile&source=1');" /><br>
     
选择文件上传2:<input name="ftp[2]" type="text" id-"ftp[2]" />
<input bype="button" name="submit1" value="浏览"  
onclick="window.open('5_3php?action=showFile&source=1');" /><br>
选择文件上传3:<input name="ftp[3]" type="text" id-"ftp[3]" />
<input bype="button" name="submit1" value="浏览"  
onclick="window.open('5_3php?action=showFile&source=1');" /><br>
</div>
<input type="button" name="addfile" value="增加文件上传框" onclick="addfilehtml();" /><br>

<br>
<input type="submit" name="submit" value="ftp上传" />
</ form>
function addFileHtml()

var form=document.getElementByID('sendfile');
var count=1;
 for(var i=0;i<form.elements.length;i++)
 {
          var name=form.elements[i].name;
          if(name.indexof("ftp") > -1 count++;
 }
 document.getElementById("filehtml").innerhtml+='选择上传文件'+ count+';
 <input name="ftp['+count+']" type="text" id="ftp['+count+'']" />
 <input type="button" name="submit ' +count+' " value="浏览" onclick="window.open(\'5_3.php?action=showFile & source=' + count +'\');" /><br>';
 </ script>
 <? php 
function showFile() {
$source=intal($_Get["source"]); //取得以get方法传递的变量
$sd=realpath("html");
if(trim($sd!=""))
{
$d=dir($sd);
while(false !==($entry=$d->read()))
{
$p=$sd.'\\'.$entry;
if(is_dir($p))
{

}
else 
{
echo "<tr><td><input type='button" onclick='selectFile(\"".$entry."'\";' value='"' 
.$entry."'</td></tr>" ;
}
}
};
d->close(); //关闭目录句柄
}
//显示javascript代码
echo '<script language="javascript">'
function selectFile(fn)
{
opender.document.getElementByid("ftp['.$source .']") .value="html/" +fn;
window.close();
}
window.close();
}
       </ script> ';
}
? >