执行 FTP 或 SFTP 命令主要使用两个选项:CURLOPT_QUOTE 和 CURLOPT_POSTQUOTE ,关于这两个选项的区别说明可参考:http://www.groad.net/bbs/read.php?tid-1642.html
下面代码会将 FTP 某个目录下的 u.php 文件命名为 u2.php ,并且还会建立一个名为 beyes 的目录。多个 FTP 或 SFTP 命令使用 curl_slist_append() 命令进行连接。
测试代码:
[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#include <curl/easy.h>
#define FTP_URL "ftp://vh492383:beyes@121.15.245.187/www/lyn/"
int main(void)
{
CURL *curl;
struct curl_slist *headerlist = NULL;
static const char ftpcommand_1[] = "RNFR u.php";
static const char ftpcommand_2[] = "RNTO u2.php";
static const char ftpcommand_3[] = "MKD beyes";
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl) {
headerlist = curl_slist_append(headerlist, ftpcommand_1);
headerlist = curl_slist_append(headerlist, ftpcommand_2);
headerlist = curl_slist_append(headerlist, ftpcommand_3);
curl_easy_setopt(curl, CURLOPT_URL, FTP_URL);
curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
curl_easy_perform(curl);
}
curl_slist_free_all (headerlist);
curl_easy_cleanup(curl);
curl_global_cleanup();
return 0;
}
上面所执行的 FTP 命令为标准命令。在平常我们的客户端中使用的命令是经过实现后的人性化命令,比如在客户端中执行文件重命名会执行 rename test.txt text.txt ,实际上 FTP 服务器会将其翻译成:
RNFR test.txt
RNTO text.txt
下面是一些标准 FTP 命令:
USER <SP> <username> <CRLF> 登录的用户名
PASS <SP> <password> <CRLF> 密码
ACCT <SP> <account-information> <CRLF>
CWD <SP> <pathname> <CRLF> 改变当前目录
CDUP <CRLF> 返回上级目录
SMNT <SP> <pathname> <CRLF>
QUIT <CRLF> 退出
REIN <CRLF> 重新登录
PORT <SP> <host-port> <CRLF>
PASV <CRLF> 获取数据传输端口
TYPE <SP> <type-code> <CRLF> 设置数据交换的类型{A(SCII),E(BCDIC),I(mage),L(ocal byte size)}
STRU <SP> <structure-code> <CRLF>
MODE <SP> <mode-code> <CRLF>
OPTS <SP> <parameter> <CRLF>
RETR <SP> <pathname> <CRLF>
STOR <SP> <pathname> <CRLF> 用覆盖方式上传一个文件到服务器
STOU <CRLF>
APPE <SP> <pathname> <CRLF> 用追加方式上传一个文件到服务器
ALLO <SP> <decimal-integer>
[<SP> R <SP> <decimal-integer>] <CRLF>
REST <SP> <marker> <CRLF> 指定重新下在的字节数
RNFR <SP> <pathname> <CRLF> 指定需要改名的原文件
RNTO <SP> <pathname> <CRLF> 指定需要改名的新文件名
ABOR <CRLF> 取消前一指令
DELE <SP> <pathname> <CRLF> 在服务器上删除指定文件
RMD <SP> <pathname> <CRLF> 在服务器上删除指定目录
MKD <SP> <pathname> <CRLF> 在服务器上创建目录
PWD <CRLF> 显示当前目录
LIST [<SP> <pathname>] <CRLF> 列出当前目录下所有文件和子目录
NLST [<SP> <pathname>] <CRLF> 列出当前目录下所有文件和子目录的名字
SITE <SP> <string> <CRLF>
SYST <CRLF>
STAT [<SP> <pathname>] <CRLF>
HELP [<SP> <string>] <CRLF> 获取帮助
NOOP <CRLF> |