通过PHP exec传递变量

问题描述 投票:3回答:4

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

我使用以下内容:

 exec ('php email.php');

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

php exec
4个回答
6
投票

传递论点:

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

在email.php中获取:

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

4
投票

使用:

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

你也可以参考:Command Line Manual


4
投票

您可以将其作为参数传递给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,从$body的内容中获取$temp_file


0
投票

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

<?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"; 
?>

complete answer(带有调用的脚本和结果)

© www.soinside.com 2019 - 2024. All rights reserved.