How can I run several PHP scripts from within another PHP script, like a batch file? I don't think include will work, if I understand what include is doing; because each of the files I'm running will redeclare some of the same functions, etc. What I want is to execute each new PHP script like it's in a clean, fresh stack, with no knowledge of the variables, functions, etc. that came before it.
如何从另一个PHP脚本中运行多个PHP脚本,如批处理文件?我不认为包含会起作用,如果我明白包括在做什么;因为我正在运行的每个文件都将重新声明一些相同的函数,等等。我想要的是执行每个新的PHP脚本,就像它在一个干净,新鲜的堆栈中,不知道变量,函数等。来到它面前。
Update: I should have mentioned that the script is running on Windows, but not on a web server.
更新:我应该提到脚本在Windows上运行,但不在Web服务器上运行。
4 个解决方案
#1
You could use the exec() function to invoke each script as an external command.
您可以使用exec()函数作为外部命令调用每个脚本。
For example, your script could do:
例如,您的脚本可以执行以下操作:
<?php
exec('php -q script1.php');
exec('php -q script2.php');
?>
Exec has some security issues surrounding it, but it sounds like it might work for you.
Exec有一些安全问题,但听起来它可能适合你。
#2
// use exec http://www.php.net/manual/en/function.exec.php
//使用exec http://www.php.net/manual/en/function.exec.php
<?php
exec('/usr/local/bin/php somefile1.php');
exec('/usr/local/bin/php somefile2.php');
?>
In the old days I've done something like create a frameset containing a link to each file. Call the frameset, and you're calling all the scripts. You could do the same with iframes or with ajax these days.
在过去,我做过类似创建包含每个文件链接的框架集的事情。调用框架集,然后调用所有脚本。这些天你可以用iframe或ajax做同样的事情。
#3
exec() is a fine function to use, but you will have to wait until termination of the process to keep going with the parent script. If you're doing a batch of processes where each process takes a bit of time, I would suggest using popen().
exec()是一个很好用的函数,但你必须等到进程终止才能继续使用父脚本。如果你正在做一批每个进程需要一些时间的进程,我建议使用popen()。
The variable you get creates a pointer to a pipe which allows you to go through a handful of processes at a time, storing them in an array, and then accessing them all with serial speed after they're all finished (much more concurrently) using steam_get_contents().
你得到的变量创建了一个指向管道的指针,它允许你一次完成一些进程,将它们存储在一个数组中,然后在它们全部完成(更多并发)之后以串行速度访问它们steam_get_contents()。
This is especially useful if you're making API calls or running scripts which may not be memory-intensive or computationally intensive but do require a significant wait for each to complete.
如果您正在进行API调用或运行可能不是内存密集型或计算密集型但需要大量等待完成每个脚本的脚本,这将非常有用。
#4
If you need any return results from those scripts, you can use the system
function.
如果需要这些脚本的任何返回结果,则可以使用系统函数。
$result = system('php myscript.php');
$otherresult = system('php myotherscript.php');
#1
You could use the exec() function to invoke each script as an external command.
您可以使用exec()函数作为外部命令调用每个脚本。
For example, your script could do:
例如,您的脚本可以执行以下操作:
<?php
exec('php -q script1.php');
exec('php -q script2.php');
?>
Exec has some security issues surrounding it, but it sounds like it might work for you.
Exec有一些安全问题,但听起来它可能适合你。
#2
// use exec http://www.php.net/manual/en/function.exec.php
//使用exec http://www.php.net/manual/en/function.exec.php
<?php
exec('/usr/local/bin/php somefile1.php');
exec('/usr/local/bin/php somefile2.php');
?>
In the old days I've done something like create a frameset containing a link to each file. Call the frameset, and you're calling all the scripts. You could do the same with iframes or with ajax these days.
在过去,我做过类似创建包含每个文件链接的框架集的事情。调用框架集,然后调用所有脚本。这些天你可以用iframe或ajax做同样的事情。
#3
exec() is a fine function to use, but you will have to wait until termination of the process to keep going with the parent script. If you're doing a batch of processes where each process takes a bit of time, I would suggest using popen().
exec()是一个很好用的函数,但你必须等到进程终止才能继续使用父脚本。如果你正在做一批每个进程需要一些时间的进程,我建议使用popen()。
The variable you get creates a pointer to a pipe which allows you to go through a handful of processes at a time, storing them in an array, and then accessing them all with serial speed after they're all finished (much more concurrently) using steam_get_contents().
你得到的变量创建了一个指向管道的指针,它允许你一次完成一些进程,将它们存储在一个数组中,然后在它们全部完成(更多并发)之后以串行速度访问它们steam_get_contents()。
This is especially useful if you're making API calls or running scripts which may not be memory-intensive or computationally intensive but do require a significant wait for each to complete.
如果您正在进行API调用或运行可能不是内存密集型或计算密集型但需要大量等待完成每个脚本的脚本,这将非常有用。
#4
If you need any return results from those scripts, you can use the system
function.
如果需要这些脚本的任何返回结果,则可以使用系统函数。
$result = system('php myscript.php');
$otherresult = system('php myotherscript.php');