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? Commented 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]);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. addslashes and strpslashes works perfect but $body = stripslashes($argv[1]) and not argv[0]
Indeed, $argv[0] is the file's name. I corrected it, thanks!
4

Use:

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

You can also refer this : Command Line Manual

Comments

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.

Comments

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)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.