5

I am using the php exec command to execute another file.

I am using the following:

 exec ('php email.php');

But I would wish to pass a variable called $body to "email.php" file through the exec command. How can I pass the variable through exec command?

1
  • php calling php? why exec()? Any reason you couldn't just include() it?
    – Spudley
    May 26, 2012 at 19:35

4 Answers 4

8

Pass the argument:

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

Get it in email.php:

$body = stripslashes($argv[1]);
2
  • Thanks. addslashes and strpslashes works perfect but $body = stripslashes($argv[1]) and not argv[0] May 26, 2012 at 19:08
  • 1
    Indeed, $argv[0] is the file's name. I corrected it, thanks! May 26, 2012 at 19:23
4

Use:

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

You can also refer this : Command Line Manual

4

You can pass it as a parameter to email.php

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

And email.php get it with

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

But if $body contains long text with fancy chars, it is better if you save it to a temporary file with random name.

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

Then in email.php, get the $body from contents of $temp_file.

0

2 ways to pass parameters to php script from php exec command:

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

see complete answer (with called script and results)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.