Created
April 20, 2026 11:36
-
-
Save sunmeat/bea2c6689a35afc97ecc2d55cc98710c to your computer and use it in GitHub Desktop.
FTP C++ example MacOS version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <fstream> | |
| #include <filesystem> | |
| #include <string> | |
| #include <curl/curl.h> | |
| using namespace std; | |
| namespace fs = std::filesystem; | |
| const string ftpHost = "ftp://f33-preview.awardspace.net"; | |
| const string ftpUserPwd = "4115733_knd241:*********************"; // замініть на ваш реальний пароль | |
| size_t WriteFileCallback(void* buffer, size_t size, size_t nmemb, void* stream) { | |
| ofstream* out = static_cast<ofstream*>(stream); | |
| size_t written = size * nmemb; | |
| out->write(static_cast<const char*>(buffer), written); | |
| return written; | |
| } | |
| size_t ReadFileCallback(void* buffer, size_t size, size_t nmemb, void* stream) { | |
| ifstream* in = static_cast<ifstream*>(stream); | |
| in->read(static_cast<char*>(buffer), size * nmemb); | |
| return in->gcount(); | |
| } | |
| bool UploadFile(const string& localPath, const string& remotePath) { | |
| if (!fs::exists(localPath)) { | |
| cerr << "Файл не знайдено: " << localPath << "\n"; | |
| return false; | |
| } | |
| CURL* curl = curl_easy_init(); | |
| if (!curl) { | |
| cerr << "Помилка ініціалізації CURL\n"; | |
| return false; | |
| } | |
| ifstream file(localPath, ios::binary); | |
| if (!file.is_open()) { | |
| cerr << "Не вдалося відкрити файл для читання: " << localPath << "\n"; | |
| curl_easy_cleanup(curl); | |
| return false; | |
| } | |
| curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); | |
| curl_easy_setopt(curl, CURLOPT_URL, (ftpHost + remotePath).c_str()); | |
| curl_easy_setopt(curl, CURLOPT_USERPWD, ftpUserPwd.c_str()); | |
| curl_easy_setopt(curl, CURLOPT_READDATA, &file); | |
| curl_easy_setopt(curl, CURLOPT_READFUNCTION, ReadFileCallback); | |
| curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); | |
| cout << "Завантаження на FTP: " << localPath << " → " << ftpHost + remotePath << "\n"; | |
| CURLcode res = curl_easy_perform(curl); | |
| curl_easy_cleanup(curl); | |
| file.close(); | |
| if (res != CURLE_OK) { | |
| cerr << "Помилка завантаження: " << curl_easy_strerror(res) << "\n"; | |
| return false; | |
| } | |
| cout << "Файл успішно завантажено: " << localPath << "\n"; | |
| return true; | |
| } | |
| bool DownloadFile(const string& remotePath, const string& localPath) { | |
| CURL* curl = curl_easy_init(); | |
| if (!curl) return false; | |
| ofstream outFile(localPath, ios::binary); | |
| if (!outFile.is_open()) { | |
| cerr << "Не вдалося відкрити файл для запису: " << localPath << "\n"; | |
| curl_easy_cleanup(curl); | |
| return false; | |
| } | |
| curl_easy_setopt(curl, CURLOPT_URL, (ftpHost + remotePath).c_str()); | |
| curl_easy_setopt(curl, CURLOPT_USERPWD, ftpUserPwd.c_str()); | |
| curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteFileCallback); | |
| curl_easy_setopt(curl, CURLOPT_WRITEDATA, &outFile); | |
| cout << "Завантаження з FTP: " << ftpHost + remotePath << " → " << localPath << "\n"; | |
| CURLcode res = curl_easy_perform(curl); | |
| curl_easy_cleanup(curl); | |
| outFile.close(); | |
| if (res != CURLE_OK) { | |
| cerr << "Помилка завантаження: " << curl_easy_strerror(res) << "\n"; | |
| return false; | |
| } | |
| cout << "Файл успішно завантажено: " << localPath << "\n"; | |
| return true; | |
| } | |
| int main() { | |
| setlocale(LC_ALL, ""); | |
| curl_global_init(CURL_GLOBAL_DEFAULT); | |
| try { | |
| // змініть шляхи під свій macOS !!! | |
| string localUpload = "/Users/твій_користувач/1/cat.jpg"; | |
| string remoteUpload = "/cat.jpg"; | |
| string remoteDownload = "/cat.jpg"; | |
| string localDownload = "/Users/твій_користувач/1/cat2.jpg"; | |
| if (UploadFile(localUpload, remoteUpload)) | |
| cout << "Завантаження на сервер успішне.\n"; | |
| if (DownloadFile(remoteDownload, localDownload)) | |
| cout << "Завантаження з сервера успішне.\n"; | |
| cout << "Операції з FTP завершено.\n"; | |
| } | |
| catch (const exception& ex) { | |
| cerr << "Помилка: " << ex.what() << "\n"; | |
| } | |
| curl_global_cleanup(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment