i have php at server side and c++ at client side.what i am trying to do is to constantly look for files on server in folder on server if there’s a file in folder then download it on client side and delete it from server folder
this will happen again and again after 5minutes if a file is present on server in folder then download it and delete from server then in c++ goto sleep again fro 5 minutes and after 5 minutes do all this again if no file present in folder on server then simply do nothing.
This requires a few steps.
- a php file that checks if a file exists and if it does maybe tell your program where to download the file (unless you know the exact name of the file already)
- a php file that returns the exact content of a file. Example:
<?php
$filename = 'public/files/file.exe';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($filename) . "\"");
readfile($filename);
In C you will have to create a new file with the right extension and write the received data to that file.
- a php file that can delete a file. With this step you will need to think about security. Before you know the whole world can delete your files. You could think about a login system or a security token.
below is my code
#define CURL_STATICLIB
#include <stdio.h>
#include <stdlib.h>
#include
//#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
How come the files on the server? Manually? FTP? or what?
manually server admin will simply copy the files from the server harddrive
So you will need a database with a table files:
id - filename - created
And a php page that gives you the content of what is in the database. I suggest in JSON format.
Your C application can extract the filename from the data.
listfiles.php
// retrieve a filelist from the database and output the data in JSON format
download.php?id=123
// retrieve the filename from the database with id=123 and grab the content of the file like i showed you above
but even if i somehow get it to working that is in first request from client side i get names of all files through some php script then i pass these names after concatenating with
string url = “httpe://localhost//Download//”+filename;
in this way i can download all files but how do i make sure or get acknowledgement that file was successfully downloaded by the client side so that afterwards it can be deleted from the server?
you’ve got the file and the checksum, so when the client is done downloading, it calculates the checksum and sends it back to your server, so you know it’s downloaded successfully.
readfile.php?file=123
… file downloaded, create checksum …
checkfile.php?file=123&checksum=abc
… file could be deleted on equal checksums …
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
#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, sizecount);
return sizecount;
}
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.
Blockquote<?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”]