通过 PHP 可执行文件传递变量


Passing variable through PHP exec

我正在使用php exec命令来执行另一个文件。

我正在使用以下内容:

 exec ('php email.php');

但是我希望通过exec命令将一个名为$body的变量传递给"email.php"文件。如何通过 exec 命令传递变量?

传递参数:

exec('php email.php "' . addslashes($body) . '"');

通过电子邮件获取.php

$body = stripslashes($argv[1]);

使用:

 exec ('php email.php firstparameter secondparameter thirdparameter');

你也可以参考这个:命令行手册

您可以将其作为参数传递给email.php

exec('php email.php "'.addslashes($body).'"');

email.php得到它

$body = stripslashes($argv[1]);

但是,如果$body包含带有花哨字符的长文本,最好将其保存到具有随机名称的临时文件中。

<?php
$temp_file = uniqid().'.txt';
file_put_contents($temp_file, $body);
exec("php email.php $temp_file");

然后在email.php中,从$temp_file的内容中获取$body

从 php exec 命令将参数传递给 php 脚本的 2 种方法:

<?php 
$fileName = '/var/www/ztest/helloworld.php 12';
$options = 'target=13';
exec ("/usr/bin/php -f {$fileName} {$options} > /var/www/ztest/log01.txt 2>&1 &");
echo "ended the calling script"; 
?>

查看完整答案(带有调用脚本和结果)