0

我用“C”写了一个程序,我想用php执行它,我尝试使用函数php_exec,但它不起作用。为了更具体地解决我的问题,我会告诉你一些细节。这个程序是用来加快下载速度的,简而言之,用户会进入一个网页,放上他们的直接链接,然后提交他们的链接以获得来自我的服务器的直接链接,这里的问题是“我怎样才能让用户使用它,有没有相当于 php_exec 的?” 因为我认为如果许多用户同时发送他们的链接,php_exec 不起作用,我也想知道,这会损害我的服务器吗?谢谢 !

4

2 回答 2

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.

于 2012-07-08T19:56:12.200 回答
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);

允许多个用户同时使用它。每个 PHP 进程都会派生出它自己的副本/path/to/executable,并将后台进程的输出刷新到用户屏幕。

于 2012-07-08T20:05:19.670 回答