php页面在后台进程中运行另一个脚本无效

时间:2017-03-03 10:27:30

标签: php exec

我已阅读此How can I run another PHP script using PHP?和其他一些来源。

所以我有2个文件。 testexec.php和testfile.php,testfile.php的目的是在表中插入一些查询。运行testexec.php的结果是成功但没有插入数据。我尝试直接访问testfile.php并插入数据。

在testexec.php中:

<?php
    ignore_user_abort(true);
    $pathFile = dirname(__FILE__) . "/textfile.php";
    $cmd = "usr/bin/php " . $pathFile;
    exec( $cmd , $output, $return);

    if(!$return) {
        echo "Fail";
    } else {
        echo "Success" . "<br/>";
    }
?>

在testfile.php中

<?php
    include("database.php");
    $myDB = new Database();

    $dateTime = new DateTime();
    $query = "INSERT INTO marking (description) VALUES ('Test Exec');";
    $result = $myDB->insertMysqli($query);
    if($result) {
        echo "success";
    } else {
        echo "fail";
    }
?>

1 个答案:

答案 0 :(得分:0)

尝试替换

$cmd = "usr/bin/php " . $pathFile;

通过

$cmd = "/usr/bin/php " . $pathFile;

并且

include("database.php");

通过

include(dirname(__FILE__) ."/database.php");