// define some variables
$local_file = 'local.zip';
$server_file = 'server.zip';
// connect to the ftp server
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// try to download
if (ftp_get($conn_id, $local_file, $server_file, ftp_BINARY)) {
echo "Successfully written to $local_file\n";
} else {
echo "There was a problem\n";
}
安装
In order to use FTP functions with your PHP configuration, you should add the --enable-ftp option when installing PHP 4 or --with-ftp when using PHP 3.
PHP 的 Windows 版本已经内置该扩展模块的支持。您无需加载任何附加的扩展库即可使用这些函数。
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_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 -- Continues retrieving/sending a file (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 -- 向服务器发送站点命令
ftp_size -- 返回指定文件的大小
ftp_ssl_connect -- Opens an Secure SSL-FTP connection
ftp_systype -- 返回远程 FTP 服务器的系统类型