- 论坛徽章:
- 0
|
[FTP] 求用php编写最短FTP.php
设置ftp的相关选项及选择上传文件名:upload.php 代码如下:
--------------------------------------------------------------------------------
<?php
$username="用户名";
$password="用户密码";
$server="主机名";
$cdir="上传目录名" ;
//以上设置你的FTP主机名、用户名和用户密码
?>;
<!-- 文件上传设置标签 -->;
<form enctype="multipart/form-data" action=ftp.php method=post>;
<!-- 传递变量 -->;
<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 $cdir; ?>;>;
<table>;
<tr>;
<td>;上传文件选择
<input type=file name=upfile>;
</td>;
</tr>;
<tr>;
<td>;
<!-- 提交表单 -->;
<input type=submit name=action value=上传>;
</td>;
</tr>;
</table>;
</form>;
--------------------------------------------------------------------------------
处理上传文件:ftp.php 代码如下:
--------------------------------------------------------------------------------
<?php
//ftp联接主机函数
function connect()
{
global $server, $username, $password;
$conn = ftp_connect($server);
ftp_login($conn, $username, $password);
return $conn;
}
//建立ftp联接
$result = connect();
if ($action == "上传"
{
//用来改变ftp路径
ftp_chdir($result, $cdir);
//用来上传指定的文件,同名并以二进制位传递
$res_code = ftp_put($result, $upfile_name, $upfile, FTP_BINARY);
// 判断是否正确上传
if ($res_code == 1)
echo "上传成功!";
else
echo "上传错误!";
}
// 关闭联接
ftp_quit($result);
?>;
这个够短了吧!!! |
|