XLI. FTP 函数

简介

FTP 函数可以帮助实现客户机与服务器之间标准文件传送协议(FTP)的规范进行文件传送。FTP 的详细定义参见 http://www.faqs.org/rfcs/rfc959 。此扩展库用来使运行的脚本对 FTP 服务器进行广泛的控制。如果只想在一个 FTP 服务器上读写文件,考虑使用 文件系统函数 中的 ftp:// wrapper ,它提供了简单和更直观的接口。

需求

要编译本扩展模块不需要外部库文件。

安装

In order to use FTP functions with your PHP configuration, you should add the --enable-ftp option when installing PHP 4 or greater or --with-ftp when using PHP 3.

PHP 的 Windows 版本已经内置该扩展模块的支持。无需加载任何附加扩展库即可使用这些函数。

运行时配置

本扩展模块在 php.ini 中未定义任何配置选项。

资源类型

在 FTP 模块中使用了一种资源类型,该资源类型为 FTP 的连接句柄,由函数 ftp_connect() ftp_ssl_connect() 产生。

预定义常量

以下常量由本扩展模块定义,因此只有在本扩展模块被编译到 PHP 中,或者在运行时被动态加载后才有效。

FTP_ASCII ( integer )

FTP_TEXT ( integer )

FTP_BINARY ( integer )

FTP_IMAGE ( integer )

FTP_TIMEOUT_SEC ( integer )

参考函数 ftp_set_option()

下列变量在 PHP 4.3.0 以后版本中被加入。

FTP_AUTOSEEK ( integer )

参考函数 ftp_set_option()

FTP_AUTORESUME ( integer )

为 GET 和 PUT 请求自动决定恢复和开始的位置 (只能工作在 FTP_AUTOSEEK 打开的情况下)

FTP_FAILED ( integer )

异步传输失败

FTP_FINISHED ( integer )

异步传输成功

FTP_MOREDATA ( integer )

异步传输是活动状态的

范例

例子 1. FTP 例子

<?php
// set up basic connection
$conn_id = ftp_connect ( $ftp_server );

// login with username and password
$login_result = ftp_login ( $conn_id , $ftp_user_name , $ftp_user_pass );

// check connection
if ((! $conn_id ) || (! $login_result )) {
        echo
"FTP connection has failed!" ;
        echo
"Attempted to connect to $ftp_server for user $ftp_user_name" ;
        exit;
    } else {
        echo
"Connected to $ftp_server, for user $ftp_user_name" ;
    }

// upload the file
$upload = ftp_put ( $conn_id , $destination_file , $source_file , FTP_BINARY );

// check upload status
if (! $upload ) {
        echo
"FTP upload has failed!" ;
    } else {
        echo
"Uploaded $source_file to $ftp_server as $destination_file" ;
    }

// close the FTP stream
ftp_close ( $conn_id );
?>

目录
ftp_alloc  -- Allocates space for a file to be uploaded
ftp_cdup  -- 切换到当前目录的父目录
ftp_chdir  -- 在 FTP 服务器上切换当前目录
ftp_chmod  -- Set permissions on a file via FTP
ftp_close  -- 关闭一个活动的 FTP 连接
ftp_connect  -- 建立一个新的 FTP 连接
ftp_delete  -- 删除 FTP 服务器上的一个文件
ftp_exec  -- 请求运行一条 FTP 命令
ftp_fget  -- 从 FTP 服务器上下载一个文件并保存到本地一个已经打开的文件中
ftp_fput  -- 上传一个已经打开的文件到 FTP 服务器
ftp_get_option  -- 返回当前 FTP 连接的各种不同的选项设置
ftp_get  -- 从 FTP 服务器上下载一个文件
ftp_login  -- 登录 FTP 服务器
ftp_mdtm  -- 返回指定文件的最后修改时间
ftp_mkdir  -- 建立新目录
ftp_nb_continue  -- 连续获取/发送文件(non-blocking)
ftp_nb_fget  -- Retrieves a file from the FTP server and writes it to an open file (non-blocking)
ftp_nb_fput  -- Stores a file from an open file to the FTP server (non-blocking)
ftp_nb_get  -- 从 FTP 服务器上获取文件并写入本地文件(non-blocking)
ftp_nb_put  -- 存储一个文件至 FTP 服务器(non-blocking)
ftp_nlist  -- 返回给定目录的文件列表
ftp_pasv  -- 返回当前 FTP 被动模式是否打开
ftp_put  -- 上传文件到 FTP 服务器
ftp_pwd  -- 返回当前目录名
ftp_quit  --  ftp_close() 的别名
ftp_raw  -- Sends an arbitrary command to an FTP server
ftp_rawlist  -- 返回指定目录下文件的详细列表
ftp_rename  -- 更改 FTP 服务器上指定的文件名
ftp_rmdir  -- 删除一个目录
ftp_set_option  -- 设置各种 FTP 运行时选项
ftp_site  -- 向服务器发送 SITE 命令
ftp_size  -- 返回指定文件的大小
ftp_ssl_connect  -- Opens an Secure SSL-FTP connection
ftp_systype  -- 返回远程 FTP 服务器的系统类型