James Obuhuma
James Obuhuma

Reputation: 407

Passing variable through PHP exec

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?

Upvotes: 5

Views: 22031

Answers (4)

tony gil
tony gil

Reputation: 9554

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)

Upvotes: 0

Samy Dindane
Samy Dindane

Reputation: 18706

Pass the argument:

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

Get it in email.php:

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

Upvotes: 8

flowfree
flowfree

Reputation: 16462

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.

Upvotes: 4

Joe
Joe

Reputation: 610

Use:

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

You can also refer this : Command Line Manual

Upvotes: 4

Related Questions