How can I run multiple php scripts from a php script (like a batch file)?

How can I run multiple PHP scripts from another PHP script, such as a batch file? I do not think inclusion will work if I understand what includes; because each of the files that I run will update some of the same functions, etc. I want every new PHP script to look like a clean fresh stack without the knowledge of the variables, functions, etc. that appeared before it.

Refresh . I should have mentioned that the script works on Windows, but not on the web server.

+7
php
source share
4 answers

You can use exec () to call each script as an external command.

For example, your script could do:

<?php exec('php -q script1.php'); exec('php -q script2.php'); ?> 

Exec has some security issues associated with it, but it looks like this might work for you.

+10
source share

// use 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 did something like creating a set of frames containing a link to each file. Call the frameset and you call all the scripts. You can do the same with iframe or ajax these days.

+6
source share

exec () is a great function to use, but you will have to wait until the process completes to continue working with the parent script. If you are running a process package where each process takes a little time, I would suggest using popen ().

The variable you get creates a pointer to the channel, which allows you to go through several processes at a time, saving them in an array, and then getting them all at a consistent speed after everything is ready (much more at the same time) using steam_get_contents ( )

This is especially useful if you are calling API calls or running scripts that may not be memory intensive or computationally intensive, but require a significant wait for each of them to complete.

+3
source share

If you need any return results from these scripts, you can use the system function.

 $result = system('php myscript.php'); $otherresult = system('php myotherscript.php'); 
+3
source share

All Articles