C++:如何使用libcurl检查FTP服务器上是否存在目录

C++: How to check if a directory exists on an FTP server using libcurl

本文关键字:是否 存在 服务器 FTP 何使用 libcurl 检查 C++      更新时间:2023-10-16

我对此束手无策。我需要检查远程FTP服务器上是否存在目录。以下是我的想法:

//ls - lists the names of the files in the remote directory
string query = "ls /public_html/somefolder/";
//prepare to send
headers = curl_slist_append(headers, query.c_str());
curl_easy_setopt(curl, CURLOPT_QUOTE, headers); 
//send query to ftp server
res = curl_easy_perform(curl);
//check result
if(res == CURLE_OK) {
    cout << "FOLDER EXISTS";
} else {
    cout << "FOLDER DOESN'T EXIST";
}

当我检查res变量包含的内容时,它会输出:

CURLE_QUOTE_ERROR(21)。

关于如何正确地做到这一点,有什么想法吗?我在谷歌上搜索了很多次。

不要尝试为此使用CURLOPT_QUOTELIST是一种数据传输,因此它作为CURLOPT_URL的一部分进行处理。

CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "ftp://ftp@ftp.gnu.org/pub/");
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
CURLcode res = curl_easy_perform(curl);
if(res == CURLE_OK) std::cout << "FOLDER EXISTSn";
else std::cout << "FOLDER DOESN'T EXISTn";