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.
Update: I should have mentioned that the script is running on Windows, but not on a web server.
Use single quotes like this $str = exec('start /B Path\to\batch. bat'); The /B means the bat will be executed in the background so the rest of the php will continue after running that line, as opposed to $str = exec('start /B /C command', $result); where command is executed and then result is stored for later use.
Executing PHP files ¶ There are three different ways of supplying the CLI SAPI with PHP code to be executed: Tell PHP to execute a certain file. Both ways (whether using the -f switch or not) execute the file my_script.
The first phase parses PHP source code and generates a binary representation of the PHP code known as Zend opcodes. Opcodes are sets of instructions similar to Java bytecodes. These opcodes are stored in memory. The second phase of Zend engine processing consists in executing the generated opcodes.
Command line scripting. You can make a PHP script to run it without any server or browser. You only need the PHP parser to use it this way. This type of usage is ideal for scripts regularly executed using cron (on *nix or Linux) or Task Scheduler (on Windows).
You could use the exec() function to invoke 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 surrounding it, but it sounds like it might work for you.
// 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'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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With