PHP的FTP学习(一)

来源:互联网 发布:淘宝情侣装店铺推荐 编辑:程序博客网 时间:2024/05/17 02:14
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
By Vikram Vaswani
Melonfire
November 07, 2000
   我们是一群PHP的忠实FANS,我们因各种不同的原因使用它-WEB站点的开发,画图,数据库的联接等 -我们发现,它非常的友好,强大而且易于使用……  
你可能已经看到PHP是怎样被用于创建GIF和JPEG图像,从数据库中动态的获取信息等等,但这只是冰山的一角---最新版本的PHP拥有着强大的文件传输功能。
在这篇教程里,我将向你展示FTP怎样通过HTTP和FTP联接来传输文件,同时也会有一些简单的程序代码,跟我来吧!

首先,你应该知道PHP是通过HTTP和FTP联接来传输文件的。通过HTTP上传文件早在PHP3中就已经出现,现在,新的FTP函数已经在新的PHP版本中出现了!
开始之前,你需要确信你的PHP支持FTP,你可以通过以下代码查知:

--------------------------------------------------------------------------------
<?

PHPinfo();

?>
--------------------------------------------------------------------------------
检查输出结果,有一“Additional Modules”区,这里列示了你的PHP支持的模块;如果你没发现FTP模块,你最好重新安装PHP,并添加FTP支持!

先让我们来看看一个典型的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服务器建立联接)、验证(确定用户是否有权力进入系统)、传输(这里包括列目录,上传或下载文件)、取消联接。

使用PHPFTP的步骤
建立一个PHPFTP联接必须遵守以下基本步骤:打开一个联接 - 发出认证信息 - 使用PHP函数操纵目录和传输文件。
以下具体实现:
--------------------------------------------------------------------------------
<?

// 联接FTP服务器
$conn = FTP_connect("FTP.server.com");

// 使用username和password登录
FTP_login($conn, "john", "doe");

// 获取远端系统类型
FTP_systype($conn);

// 列示文件
$filelist = FTP_nlist($conn, ".");

// 下载文件
FTP_get($conn, "data.zip", "data.zip", FTP_BINARY);

// 关闭联接
FTP_quit($conn);

?>
--------------------------------------------------------------------------------
让我们一步步的来:
为了初结化一个FTP联接,PHP提供了FTP_connect()这个函数,它使用主机名称和端口作为参数。在上面的例子里,主机名字为“FTP.server.com”;如果端口没指定,PHP将会使用“21”作为缺省端口来建立联接。
联接成功后FTP_connect()传回一个handle句柄;这个handle将被以后使用的FTP函数使用。
--------------------------------------------------------------------------------
<?

// connect to FTP server
$conn = FTP_connect("FTP.server.com");

?>
--------------------------------------------------------------------------------
一旦建立联接,使用FTP_login()发送一个用户名称和用户密码。你可以看到,这个函数FTP_login()使用了FTP_connect()函数传来的handle,以确定用户名和密码能被提交到正确的服务器。
--------------------------------------------------------------------------------
<?

// log in with username and password
FTP_login($conn, "john", "doe");

?>
--------------------------------------------------------------------------------
这时,你就能够做你想做的事情了,具体在下一部分讲:

做完你想做的事后,千万要记住使用FTP_quit()函数关闭你的FTP联接
  
--------------------------------------------------------------------------------
<?

// close connection
FTP_quit($conn);

?>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击