问题
I think I'm supposed to use CURLOPT_DIRLISTONLY
but I'm not really sure how to proceed.
CURLcode curl_easy_setopt(CURL *handle, CURLOPT_DIRLISTONLY, long listonly);
I'm not sure how to make the call to actually print the list.
回答1:
Enabling the CURLOPT_DIRLISTONLY
option for FTP has the effect of retrieving only file names instead of file details (e.g. file size, date, etc.) basically as the difference between using a plain ls
(only names) or a ls -l
(listing with details) on UNIX. At the FTP protocol level, enabling CURLOPT_DIRLISTONLY
will make libcurl issue a NLST
command rather than a LIST
. Then to get the directory listing you do the same as a file transfer, with the difference that your ftp://
URL will point to a directory (and not to a file). The contents of that transfer will be your directory listing.
To display the directory listing, rather than saving it to a file, you can use the CURLOPT_WRITEFUNCTION
option to have libcurl call a callback function you supply for every block of data. That function only needs to fwrite
the data to stdout
to display it. It should look something like this:
void write_callback(void* data, size_t size, size_t nmemb, void* ptr)
{
fwrite(data, size, nmemb, stdout);
}
int main()
{
// ...
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_callback);
// ...
ret = curl_easy_perform(handle);
}
来源:https://stackoverflow.com/questions/34279893/how-do-i-use-libcurl-to-printf-a-remote-ftp-directory-listing