Jump to content

Recommended Posts

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.

You will need to turn your thinking around. You need the client to periodically query the server and have the server tell the client if there is a file to download. Then the client can download the file from the server and when complete the server can delete the file.

7 minutes ago, gw1500se said:

You will need to turn your thinking around. You need the client to periodically query the server and have the server tell the client if there is a file to download. Then the client can download the file from the server and when complete the server can delete the file.

yes that's what i am planning to do simply check again and again via thread in c++ as c++ is client side it will check after 5 minutes if a files is present in server for downloading then download it to client and after successfull download to client side delete it from server folder and go back to sleep.

but how do i do that from php..i can handle c++ code but i don't know how to do php server side coding

This is a help forum not a free coding forum, if that is what you are asking. You need to do the work and if you have problems, show what you did, what you expect and what is wrong. A summary of what you want is to write a PHP script that is launched by the client via curl. That script would then check the directory for a file(s) and if present start the download. Once complete the client would confirm the successful download and have the PHP script delete the file.

Edited by gw1500se
13 minutes ago, gw1500se said:

This is a help forum not a free coding forum, if that is what you are asking. You need to do the work and if you have problems, show what you did, what you expect and what is wrong. A summary of what you want is to write a PHP script that is launched by the client via curl. That script would then check the directory for a file(s) and if present start the download. Once complete the client would confirm the successful download and have the PHP script delete the file.

you are telling what i already have said...

1 minute ago, requinix said:

How are the files getting onto the server in the first place?

these files are dropped in the server folder by the server admin.now i don't have any experience in php so i don't know how to send files from php server using php script..i can handle client side i.e c++ code i can send request again and again after specified time for eg after every 5 minutes i can send request from client side.but server side is supose to check if files are present in folder if yes then send to client side and delete from server folder if no then do nothing 

2 minutes ago, jazz15 said:

these files are dropped in the server folder by the server admin.

So they're... uploaded? Using the site or FTP? Or are created directly on the server? I'm looking for a specific answer here because your requirement to delete the file depends partly on the files and how they get there.

13 minutes ago, requinix said:

So they're... uploaded? Using the site or FTP? Or are created directly on the server? I'm looking for a specific answer here because your requirement to delete the file depends partly on the files and how they get there.

directly copied from the server pc to the folder

i got this simple code but the problem is with this methodology i have to hardcode filename in the url section but i don't want to do that as i need to download every file from server to my client side without knowing the name of the file

/***************************************************************************
 *                                  _   _ ____  _
 *  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 page.out.
 * </DESC>
 */ 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <curl/curl.h>

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 = "page.out";
  FILE *pagefile;

  if(argc < 2) {
    printf("Usage: %s <URL>\n", argv[0]);
    return 1;
  }

  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, argv[1]);

  /* 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;
}

 

copied from the server pc to the folder - on what?  What folder on what device are you talking about.

 

If this item is being placed onto the server for you to then download to the client - yes you will have to execute the script on the client somehow.  OTOH - if it is on the server just waiting to be processed by something, why not schedule a cron job to run on the server every x minutes to actually do that process?  You could then do something on the client that does a check of what results have been produced.

2 minutes ago, ginerjm said:

copied from the server pc to the folder - on what?  What folder on what device are you talking about.

 

If this item is being placed onto the server for you to then download to the client - yes you will have to execute the script on the client somehow.  OTOH - if it is on the server just waiting to be processed by something, why not schedule a cron job to run on the server every x minutes to actually do that process?  You could then do something on the client that does a check of what results have been produced.

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.

So you're basically writing a transfer mechanism to take a file from someone who puts on the server and you want it on a client. 

What about an email with an attachment that you process using a script that handles that inbox?

7 minutes ago, ginerjm said:

So you're basically writing a transfer mechanism to take a file from someone who puts on the server and you want it on a client. 

What about an email with an attachment that you process using a script that handles that inbox?

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

1 hour ago, gw1500se said:

When your client contacts the server, have the PHP script return the names of any files that are ready to download. Then have the client request the download of those files.

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++

Edited by jazz15
2 minutes ago, gw1500se said:

You're kidding, right? Try 'echo'.

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++

6 hours ago, jazz15 said:

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++

Side note: glob() returns the path to each file according to the pattern you gave it. So all those filenames will look like /path/to/directory/foo.txt. What the client (C++) needs to know is the URL to hit on the server; if every file is in the same directory (which it sounds like it is) then you can get just the file names with basename().

Since you're working from C++, and by the way this is all something doable scriptable a shell and without the use of a compiled program, you'll want to make this all as simple as possible. The server should return just a plaintext list of filenames. You can read each line, get the filename, then download the file.

<?php

header("Content-Type: text/plain");

foreach (glob("/path/to/directory/*.txt") as $path) {
    echo basename($path), "\n";
}

You still have the problem of having to send an HTTP request to the server and reading the response...

  • Like 1
13 hours ago, requinix said:

Side note: glob() returns the path to each file according to the pattern you gave it. So all those filenames will look like /path/to/directory/foo.txt. What the client (C++) needs to know is the URL to hit on the server; if every file is in the same directory (which it sounds like it is) then you can get just the file names with basename().

Since you're working from C++, and by the way this is all something doable scriptable a shell and without the use of a compiled program, you'll want to make this all as simple as possible. The server should return just a plaintext list of filenames. You can read each line, get the filename, then download the file.


<?php

header("Content-Type: text/plain");

foreach (glob("/path/to/directory/*.txt") as $path) {
    echo basename($path), "\n";
}

You still have the problem of having to send an HTTP request to the server and reading the response...

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?

14 minutes ago, gw1500se said:

Your questions seem to be going in circles. Lets back up and and tell us how you are using curl in your c++ program. I assume you are using the c++ curl library, right?

yes i have posted the exact code above which is in c++

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.