0

I have written a program with " C ", and I would like to execute it with php, I tried to use the function php_exec, but It doesn't work. To be more specific in my problem I will tell you some details. This program is used to make downloads faster, in brief, the users will enter to a webpage, a put their direct links, then submit their links to have a direct link from my server, the problem here is that " How can I can let the user use it, is there any equivalent to php_exec ? " Because I think that php_exec does not work if many users send their links in the same time, also I would like to know, can this harm my server ? Thank you !

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439

2 Answers2

3

You probably do not want to use exec in that situation since it waits until the executed program is finished which blocks the complete script.

The PHP Documentation tells you the following:

Note: If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

However, you probably want to add the links to a queue (maybe a database) and execute a cron job every now and then which then downloads the files which are stored in the queue.

There is also another similar question with an excellent answer.

Community
  • 1
  • 1
fdomig
  • 4,417
  • 3
  • 26
  • 39
1
// Start your 'downloader'
$handle = popen('/path/to/executable 2>&1', 'r');
// While it's generating output, print it to the screen
while (!feof($handle)){
    echo fread($handle, 2096);
}
// Close the process handle
pclose($handle);

This will allow multiple users to use it at the same time. Each PHP process will fork it's own copy of /path/to/executable, and will enable flushing of the output of the background process to the users screen.

Jay
  • 3,285
  • 1
  • 20
  • 19