php连接FTP服务器
发布时间:2010-08-20 07:55 点击量:

你想了解PHP怎么连接FTP服务器吗?呵呵.请关注www.chinaitweb.com这个网站
1. 连接FTP服务器的函数ftp_connect()
如果服务器连接成功则返回一个连接对象,否则返回FALSE
resource ftp_connect(string $host [,int $port[,int $timeout]]);
host:主机IP地址或者名称
port:主机对应的端口号,如果不添默认为21
timeour:此参数为可选的,用来设置网络传输超时时间,如果不指定,那么默认为90秒
2. 登寻FTP的函数 ftp_login()
该函数的返回值结果是布尔类型的true或false
bool ftp_login(resource $ftp_stream,string $username,string $password)
$ftp_stream:已获得的FTP服务连接
$username:登陆FTP帐号的用户名
$password:登陆FTP的密码
例子:connection.php
<?php
//连接数据库
$host="127.0.0.1";
$port=21;
$timeout=30;
//连接FTP服务器
$connection = ftp_connect($host,$port,$timeout);
//获得结果
if($connection)
{
echo "服务器连接成功!<br>";
}else {
echo "服务器连接失败!";
}
//登录数据库
$username="admin";
$pwd="admin";
$result = ftp_login($connection,$username,$pwd);
if($result)
{
echo "用户".$username."登录成功!";
}else {
echo "登录失败!";
}
function getConnection($host,$port){
//连接FTP服务器
$timeout=30;
//连接FTP服务器
$connection = ftp_connect($host,$port,$timeout);
//获得结果
if($connection)
{
echo "服务器连接成功!<br>";
}else {
echo "服务器连接失败!";
}
return $connection;
}
?>
获取FTP服务器文件列表
array ftp_nlist(resource $ftp_stream,string $directory)
$ftp_stream:FTP服务器连接对象
$directory:列举文件目录的文件夹名称
例子:file.php
<?php
//连接数据库
$host="127.0.0.1";
$port=21;
$timeout=30;
//连接FTP服务器
$connection = ftp_connect($host,$port,$timeout);
//获得结果
if($connection)
{
echo "服务器连接成功!<br>";
}else {
echo "服务器连接失败!";
}
//登录FTP服务器
$username="admin";
$pwd="admin";
$result = ftp_login($connection,$username,$pwd);
if($result)
{
echo "用户".$username."登录成功!";
}else {
echo "登录失败!";
}
$filelist = ftp_nlist($connection, ""); //列出服务器上全部文件
if ($filelist!=null) {
echo "<br>";
for ($i=0;$i<count($filelist);$i++){
print_r("服务器文件:".$filelist[$i]);
print_r(" 大小为:".ftp_size($connection,$filelist[$i])."<br>");
}
}else {
echo "读取文件失败";
}
ftp_quit($connection);
?>
上传文件:
bool ftp_fput(resource $ftp_stream,string $remote_file,resource $handle,int $mode [,int $startpos])
$ftp_stream:打开的FTP连接
$remote_file:上传到服务器的名称
$handle:要上传的文件名称
$mode:上传模式 只能是文本模式(ftp_ascii)或二进制(ftp_binary)
如上传text.txt文件到ftp服务器上如下:
例:upload.php
<?php
include("connection.php");
$conn=getConnection("127.0.0.1","21");
//登录服务器
$result = ftp_login($conn,"admin","admin");
if($result)
{
echo "用户".$username."登录成功!";
if (ftp_put($conn,"newFile.txt","d://text.txt",FTP_BINARY )) {
echo "文件上传成功!";
}else {
echo "文件上传失败!";
}
}else {
echo "登录失败!";
}
//关闭服务器连接
ftp_quit($connection);
?>
下载文件:
bool ftp_fget(resource $ftp_stream, ,resource $handle,string $remote_fileint $mode [,int $startpos])
$ftp_stream:打开的FTP连接
$remote_file:要下载的文件名称
$handle打开的文件句柄
$mode:下载模式 只能是文本模式(ftp_ascii)或二进制(ftp_binary)
下载FTP服务器中的AA。TXT文件到本地磁盘的代码如下:
<?php
include("connection.php");
$conn=getConnection("127.0.0.1","21");
//登录服务器
$result = ftp_login($conn,"admin","admin");
if($result)
{
echo "用户".$username."登录成功!";
$file = 'd://ftpServer/aa.txt';
$fp = fopen($file, 'r+');
if (ftp_fget($conn,$fp,"aa.txt",FTP_ASCII)) {
echo "文件下载成功!";
}else {
echo "文件下载失败!";
}
fclose($fp);
}else {
echo "登录失败!";
}
//关闭服务器连接
ftp_quit($connection);
?>
删除FTP服务器中文件
bool ftp_delete(resource $ftp_stream,string $path)
$ftp_stream:打开的FTP连接
$path: 删除的文件名称
删除FTP服务器中的aa.txt文件代码如下:
<?php
include("connection.php");
$conn=getConnection("127.0.0.1","21");
//登录服务器
$result = ftp_login($conn,"admin","admin");
if($result)
{
echo "用户".$username."登录成功!";
if (ftp_delete($conn,"aa.doc")) {
echo "文件删除成功!";
}else {
echo "文件删除失败!";
}
}else {
echo "登录失败!";
}
//关闭服务器连接
ftp_quit($connection);
?>
(责任编辑:admin)