Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run several PHP scripts from within a PHP script (like a batch file)?

Tags:

php

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.

like image 580
Charles Avatar asked May 21 '09 18:05

Charles


People also ask

How do I run a batch file in PHP?

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.

How many ways in which PHP script can be executed which?

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.

How PHP scripts are executed?

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.

Can you write scripts in PHP?

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).


2 Answers

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.

like image 69
zombat Avatar answered Sep 20 '22 00:09

zombat


// 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.

like image 28
artlung Avatar answered Sep 22 '22 00:09

artlung