我使用PHP的exec命令來執行另一個文件。傳遞變量throght PHP的exec
我使用以下內容: EXEC( 'PHP email.php');
但我希望通過exec命令通過一個名爲$體「email.php」文件變量。我如何通過exec命令傳遞變量?
我使用PHP的exec命令來執行另一個文件。傳遞變量throght PHP的exec
我使用以下內容: EXEC( 'PHP email.php');
但我希望通過exec命令通過一個名爲$體「email.php」文件變量。我如何通過exec命令傳遞變量?
傳遞參數:
exec('php email.php "' . addslashes($body) . '"');
得到它email.php:
$body = stripslashes($argv[1]);
謝謝。 addslashes和strpslashes完美,但$ body = stripslashes($ argv [1])而不是argv [0] –
確實,'$ argv [0]'是文件名。我糾正它,謝謝! –
用途:
exec ('php email.php firstparameter secondparameter thirdparameter');
您也可以參考這個:Command Line Manual
你可以把它作爲一個參數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
。
2種方式從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(帶有調用腳本和結果)
php調用php?爲什麼'exec()'?任何你不能「包括」的理由? – Spudley