Jump to content

jazz15

Members
  • Content Count

    21
  • Joined

  • Last visited

Community Reputation

0 Neutral

About jazz15

  • Rank
    Member
  1. so it means i need to send a request again on the server for e.g on result.php after downloading the file that file was successfully downloaded at client's pc?after that i can have a script in result.php that gets filename and size from client pc which was downloaded on client pc and write it to some text file that this file with blah size was downloaded at blah blah time by the cleint's pc
  2. some way to acknowledge on server that file was downloaded by the client...i can do this with calling another php script but i want some method to send acknowledgement through the same request in which client side is going to download the file..let me work on that and i,ll get back with full code
  3. i intialized it like this $send=array(); or even with this initialization $send=[]; it won't work because syntax is not correct
  4. actually i posted the above screenshot to show that this is what i get with my php script..when i changed it with $send+ like you have posted above it showed only the first record
  5. this is the proper way to send names of files along with the size of files #define CURL_STATICLIB //#define JSON_DLL using namespace std; #include <cstdint> #include <iostream> #include <memory> #include <string> #include <curl.h> #include<json\reader.h> #include <json/json.h> #pragma warning( disable : 4996) namespace { std::size_t callback( const char* in, std::size_t size, std::size_t num, std::string* out) { const std::size_t totalBytes(size * num); out->append(in, totalBytes); return totalBytes; } } int main() { const std::string url("http://localhost/file.php"); CURL* curl = curl_easy_init(); // Set remote URL. curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // Don't bother trying IPv6, which would increase DNS resolution time. curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); // Don't wait forever, time out after 10 seconds. curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); // Follow HTTP redirects if necessary. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // Response information. long httpCode(0); std::unique_ptr<std::string> httpData(new std::string()); // Hook up data handling function. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback); // Hook up data container (will be passed as the last parameter to the // callback handling function). Can be any pointer type, since it will // internally be passed as a void pointer. curl_easy_setopt(curl, CURLOPT_WRITEDATA, httpData.get()); // Run our HTTP GET command, capture the HTTP response code, and clean up. curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode); curl_easy_cleanup(curl); if (httpCode == 200) { std::cout << "\nGot successful response from " << url << std::endl; // Response looks good - done using Curl now. Try to parse the results // and print them out. Json::Value jsonData; Json::Reader jsonReader; // Json::arrayValue jsonArray; if (jsonReader.parse(*httpData.get(), jsonData)) { for (Json::Value::ArrayIndex i = 0; i != jsonData.size(); i++) { std::cout << "Successfully parsed JSON data" << std::endl; std::cout << "\nJSON data received:" << std::endl; std::cout << jsonData.toStyledString() << std::endl; const std::string Name(jsonData[i]["Name"].asString()); const std::string Size(jsonData[i]["Size"].asString()); std::cout << "Natively parsed:" << std::endl; std::cout << "\tName: " << Name << std::endl; std::cout << "\tSize: " << Size << std::endl; std::cout << std::endl; } } else { std::cout << "Could not parse HTTP data as JSON" << std::endl; std::cout << "HTTP data was:\n" << *httpData.get() << std::endl; return 1; } } else { std::cout << "Couldn't GET from " << url << " - exiting" << std::endl; return 1; } return 0; } and this is the php script <?php $send=array(); $size=array(); $files = glob("Downloads/*.txt"); foreach ($files as $key => $val) { $send[]=array('Name'=>basename($val),'Size'=>filesize($val)); } if(!empty($send)) { echo json_encode($send); } else{ echo 'empty!'; } ?>
  6. i didn't get that so this is what i did which is not good but it is what it is... char chars[] = "[]\""; for (unsigned int i = 0; i < strlen(chars); ++i) { // you need include <algorithm> to use general algorithms like std::remove() response.erase(std::remove(response.begin(), response.end(), chars[i]), response.end()); } string ter; const char s[2] = ","; char *token; char* c = const_cast<char*>(response.c_str()); /* get the first token */ token = strtok(c, s); /* walk through other tokens */ while (token != NULL) { printf(" %s\n", token); int i = 0; ter.append(token); ter.append("\n"); token = strtok(NULL, s); } istrstream uy(ter.c_str()); string line; while (std::getline(uy, line)) { cout << line<<endl; } now i need some kind of affirmation from the client side to the server side after successfully downloading the file on client side so that file on server folder can be deleted.or is there any other good method for this affirmation so i get acknowledgement that file was successfully downloaded on client pc?
  7. <?php $send=array(); $files = glob("path/Downloads/*.txt"); foreach ($files as $key => $val) { $send[]= basename($val); } if(!empty($send)) { echo json_encode($send); } else{ echo 'empty!'; } ?> so far i am able to get all names of .txt files in my c++ client side in jsonencoded form..now only thing left is to store these filenames in an array in c++.after which i can focus on how to get acknowledgement that file was successfully downloaded by the client so that it can be deleted from the server safely this is the format i am getting ["text2.txt","text3.txt","text4.txt"]
  8. here is the code but the issue is i want to return an array containing names how do i get it in c++ in array form so i can iterate over it one by one. #define CURL_STATICLIB #include <iostream> #include <stdlib.h> #include <stdio.h> #include <curl.h> using namespace std; size_t size = 0; size_t write_to_string(void *ptr, size_t size, size_t count, void *stream) { ((string*)stream)->append((char*)ptr, 0, size*count); return size*count; } int main() { CURL *curl; CURLcode res; curl_global_init(CURL_GLOBAL_ALL); curl = curl_easy_init(); if (curl) { string out = string("htt://file.php"); curl_easy_setopt(curl, CURLOPT_URL, out.c_str()); string response; curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_string); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); res = curl_easy_perform(curl); cout << "Variable response : " << response.c_str(); if (res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); curl_easy_cleanup(curl); } curl_global_cleanup(); system("pause"); return 0; } <?php //$Aid = $_REQUEST['Aid']; //$Acd = $_REQUEST['Acd']; $Aid='test'; $Acd='test2'; $data = array(); $data[] = array('Aid_response'=> $Aid, 'Acd_response'=> $Acd); //echo json_encode($data); ?> this php script is just for testing data.ultimately i will be sending names of the files available in download folder on server and then i will be passing these file names in another post request to download the file.
  9. yes i have posted the exact code above which is in c++
  10. so in first request i can request for filenames but how do i get these filenames in c++ through curl get request?second issue which i was thinking of is that if somehow i get names successfully then i can download it by passing names in the url but how do i delete these files from server once they are finally downloaded by the client successfully?or is there any other method or way for acknowledgement that files was successfully downloaded by the client c++ side?
  11. no i mean i can use this $files = glob("/path/to/directory/*.txt"); to find al .tx files and store it in $files array now how do i send this $files variable data to my client side which is in c++
  12. yes that makes sense..but how do i send the filenames from php script?i don't know how to do that. i can do the second part i.e i can make client request to download the files but i can't do the first part i.e send file names from php script to client in c++
  13. no that' not required for my case. here's full working code you can see its written in c++ console application empty project on visual studio 2015 /*************************************************************************** * _ _ ____ _ * Project ___| | | | _ \| | * / __| | | | |_) | | * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms * are also available at https://curl.haxx.se/docs/copyright.html. * * You may opt to use, copy, modify, merge, publish, distribute and/or sell * copies of the Software, and permit persons to whom the Software is * furnished to do so, under the terms of the COPYING file. * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * KIND, either express or implied. * ***************************************************************************/ /* <DESC> * Download a given URL into a local file named info.txt. * </DESC> */ #define CURL_STATICLIB #include <stdio.h> #include <stdlib.h> #include<string> //#include <unistd.h> #include <curl.h> #pragma warning(disable : 4996) using namespace std; static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) { size_t written = fwrite(ptr, size, nmemb, (FILE *)stream); return written; } int main(int argc, char *argv[]) { CURL *curl_handle; static const char *pagefilename = "info.txt"; FILE *pagefile; string url = "http://localhost//Download//info.txt"; curl_global_init(CURL_GLOBAL_ALL); /* init the curl session */ curl_handle = curl_easy_init(); /* set URL to get here */ curl_easy_setopt(curl_handle, CURLOPT_URL, url); /* Switch on full protocol/debug output while testing */ curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L); /* disable progress meter, set to 0L to enable and disable debug output */ curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L); /* send all data to this function */ curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data); /* open the file */ pagefile = fopen(pagefilename, "wb"); if (pagefile) { /* write the page body to this file handle */ curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile); /* get it! */ curl_easy_perform(curl_handle); /* close the header file */ fclose(pagefile); } /* cleanup curl stuff */ curl_easy_cleanup(curl_handle); curl_global_cleanup(); return 0; } you can see my only issue is that i can't use hardcoded filename to supply and copy from server i need some way to automatically get the files which are in download folder and then download it to the client side and delete from the server after successfully downloading to client side..right now this file is info.txt but instead of hardcoding i want some way to provide string url = "http://localhost//Download//findandupload.php"; this url and then in findandupload.php i need to perform this task that find all files in Download folder and send to client side and delete afterwords from server
  14. server pc..files are on server pc ike sometext.txt file and i want to download it to client side using curl...i posted code for that above but also mentioned the issue.and client side is in c++ which is suppose to check after every 5 minutes...client side will send request after every 5 minutes and server side php script will check if any file is present inside the folder on server if yes then cleint side will download it to client pc and remove from the server side from the server's folder.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.