先让我们来看看一个典型的FTP任务是怎样完成的吧!
--------------------------------------------------------------------------------
$ ftp ftp.server.com
Connected to ftp.server.com
220 server.com FTP server ready.
Name (server:john): john
331 Password required for john.
Password:
230 User john logged in.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
200 PORT command successful.
150 Opening ASCII mode data connection for /bin/ls.
drwxr-xr-x 5 john users 3072 Nov 2 11:03 .
drwxr-xr-x 88 root root 2048 Nov 1 23:26 ..
drwxr--r-- 2 john users 1024 Oct 5 13:26 bin
drwx--x--x 8 john users 1024 Nov 2 10:59 public_html
drwxr--r-- 4 john users 1024 Nov 2 11:26 tmp
-rw-r--r-- 1 john users 2941465 Oct 9 17:21 data.zip
226 Transfer complete.
ftp> bin
200 Type set to I.
ftp> get data.zip
local: data.zip remote: data.zip
200 PORT command successful.
150 Opening BINARY mode data connection for data.zip(2941465 bytes).
226 Transfer complete.
ftp> bye
221 Goodbye.
--------------------------------------------------------------------------------
你可以看到,进程明显被分为几段:联接(与FTP服务器建立联接)、验证(确定用户是否有权力进入系统)、传输(这里包括列目录,上传或下载文件)、取消联接。
--------------------------------------------------------------------------------
<?
// close connection
ftp_quit($conn);
?>
--------------------------------------------------------------------------------
登录了FTP服务器,PHP提供了一些函数,它们能获取一些关于系统和文件以及目录的信息。
ftp_pwd()
如果你想知道你当前所在的目录时,你就要用到这个函数了。
--------------------------------------------------------------------------------
<?
// get current location
$here = ftp_pwd($conn);
?>
--------------------------------------------------------------------------------
万一你需要知道服务器端运行的是什么系统呢?
ftp_systype()正好提供给你这方面的信息。
--------------------------------------------------------------------------------
<?
// get system type
$server_os = ftp_systype($conn);
?>
--------------------------------------------------------------------------------
关于被动模式(PASV)的开关,PHP也提供了这样一个函数,它能打开或关闭PASV(1表示开)
--------------------------------------------------------------------------------
<?
// turn PASV on
ftp_pasv($conn, 1);
?>
--------------------------------------------------------------------------------
现在,你已经知道你在“哪里”和“谁”跟你在一起了吧,现在我们开始在目录中逛逛--实现这一功能的是ftp_chdir()函数,它接受一个目录名作为参数。
--------------------------------------------------------------------------------
<?
// change directory to "public_html"
ftp_chdir($conn, "public_html");
?>
--------------------------------------------------------------------------------
如果你想回到你刚才所在的目录(父目录),ftp_cdup()能帮你实现你的愿望,这个函数能回到上一级目录。
--------------------------------------------------------------------------------
<?
// go up one level in the directory tree
ftp_cdup($conn);
?>
--------------------------------------------------------------------------------
你也能够建立或移动一个目录,这要使用ftp_mkdir()和ftp_rmdir()函数;注意:ftp_mkdir()建立成功的话,就会返回新建立的目录名。
--------------------------------------------------------------------------------
<?
// make the directory "test"
ftp_mkdir($conn, "test");
// remove the directory "test"
ftp_rmdir($conn, "test");
?>
--------------------------------------------------------------------------------
建立一个FTP的目录通常是传输文件--- 那么就让我们开始吧!