简体   繁体   English

如何从一个PHP脚本批量执行PHP脚本?

[英]How to batch execute PHP scripts from one PHP script?

I have a series of PHP scripts that I want to run in a particular order. 我有一系列要按特定顺序运行的PHP脚本。 I tried using 我尝试使用

<?php
    exec('file1.php');
    exec('file2.php');
    exec('file3.php');
?>

to accomplish this, but just got a series of errors. 为此,但出现了一系列错误。 If I run them from the command line, they all work fine. 如果我从命令行运行它们,它们都可以正常工作。 How to fix this problem? 如何解决这个问题?

If the state of each script is well isolated (ie not clashing function/class names and global variables), you can just include each of them in turn. 如果每个脚本的状态被很好地隔离(即,不冲突函数/类名和全局变量),则可以依次包含每个脚本。

include("file1.php");
include("file2.php");
...

This will also ensure you don't spin up multiple PHP interpreters. 这还将确保您不会启动多个PHP解释器。

You can run it from the command line from your scripts, assuming you have root access. 如果您具有root用户访问权限,则可以从脚本的命令行运行它。

Example: 例:

<?php
    system("php -f path/to/your/script/file1.php");
    system("php -f path/to/your/script/file2.php");
    system("php -f path/to/your/script/file3.php");
?>

I haven't tested it, but it should work :) 我还没有测试过,但它应该可以工作:)

system('php file1.php')

或者,仅在* nix上使用shell脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM