I’m using libcurl for FTP. My code seemed to be running fine and files were uploaded as I expected. But after a while, I found the connection went wrong and the error message is here.
FTP Folder Error:
Windows cannot access this folder. Make sure you typed the file name correctly and you have
permission to access the folder.
Details:
The connection with the server was reset.
I asked my colleague to help fix the server yesterday and my code could upload files again. However, this problem occurred again this morning and I have to wonder if there is something wrong with my code. Here is my code.
#include <iostream>
#include <curl/curl.h>
bool createFtpDirectory(const std::string& url, const std::string& userpwd) {
CURL* curl;
CURLcode res;
bool success = false;
curl = curl_easy_init();
if (curl) {
// Set the URL for the FTP operation
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
// Specify the username and password for the FTP server
curl_easy_setopt(curl, CURLOPT_USERPWD, userpwd.c_str());
// Specify the FTP command to create a directory
curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, CURLFTP_CREATE_DIR);
// Perform the request
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
}
else {
success = true;
}
// Clean up
curl_easy_cleanup(curl);
}
return success;
}
bool uploadFileToFtp(const std::string& url, const std::string& userpwd, const std::string& localFilePath) {
CURL* curl;
CURLcode res;
FILE* hd_src;
bool success = false;
// Open the file to be uploaded
errno_t err = fopen_s(&hd_src, localFilePath.c_str(), "rb");
if (err != 0 || !hd_src) {
std::cerr << "Failed to open file: " << localFilePath << std::endl;
return false;
}
curl = curl_easy_init();
if (curl) {
// Set the URL for the FTP operation
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
// Specify the username and password for the FTP server
curl_easy_setopt(curl, CURLOPT_USERPWD, userpwd.c_str());
// Specify the read callback function
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
// Perform the request
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
}
else {
success = true;
}
// Clean up
curl_easy_cleanup(curl);
}
fclose(hd_src);
return success;
}
int main() {
const std::string ftpUrl = "ftp://MY_FTP_PATH";
const std::string ftpFileUrl = "ftp://MY_FILE_PATH/template.xlsx";
const std::string userpwd = "USER:PWD";
const std::string localFilePath = "template.xlsx";
// Create directory on FTP server
if (createFtpDirectory(ftpUrl, userpwd)) {
std::cout << "Directory created successfully." << std::endl;
}
else {
std::cerr << "Failed to create directory." << std::endl;
return 1;
}
// Upload file to FTP server
if (uploadFileToFtp(ftpFileUrl, userpwd, localFilePath)) {
std::cout << "File uploaded successfully." << std::endl;
}
else {
std::cerr << "Failed to upload file." << std::endl;
return 1;
}
return 0;
}
I hope someone can point out the loopholes in my code, or give me some other ideas to solve this problem.
Thanks in advance to everyone who sees this question.