tags:

views:

428

answers:

12

neither

<?php system('/usr/bin/php file.php'); ?>

I tried with -q, with !#/usr/bin/php etc... anyone can help?

A: 

Without any more info... it's probably the path to the PHP executable, the path to file.php or a file permissions problem.

Greg
+4  A: 

You are supposed to call it with -f, but it should work without it as well:

<?php system('/usr/bin/php -f file.php'); ?>

What do you mean by "it doesn't work"?

Did you want the contents to be outputted as if they were from your script? Use include or require.

Did you want the contents in a variable? Use the backtick operator.

You can see what you get back from the command by using the backtick operator instead of system.

Vegard Larsen
A: 

What happens when you pass in a full path to 'file.php'?

Also, try to redirect the output (but stdout and stderr) to a file so that you can see the error messages that are generated.

Roel
+1  A: 

Could be due to safe mode.

Note: When safe mode is enabled, you can only execute files within the safe_mode_exec_dir. For practical reasons, it is currently not allowed to have .. components in the path to the executable.

http://www.php.net/system.

Tahir Akhtar
A: 

Thanks all for answering!!

For 'doesn't work' I meant: it does not return or print anything.

I have two files a.php && b.php (with all permissions) and safe_mode is off

b.php

<?php
$a = system('/usr/bin/php -f /Applications/MAMP/htdocs/a.php',$b);
print_r($a);
echo '-'; # for separation
print_r($b); ?>

and a.php

<?php echo 'hello world'; ?>

and when I run b.php from my browser (localhost/b.php) it prints:

string(0) "" -int(5)

that means $b variable is 5 but... 5 what?

barredo
Hi Barrendo, stackoverflow isn't like a forum. You should really edit your original question and add this information so it's easier to find.
Gary Richardson
A: 

What does it say when you turn on error reporting?

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$a = system('/usr/bin/php -f /Applications/MAMP/htdocs/a.php',$b);
print_r($a);
echo '-'; # for separation
print_r($b);
olle
same thing, doesn't seem to fail
barredo
A: 

When I change

system('/usr/bin/php -f /Applications/MAMP/htdocs/a.php',$b);

for

('/bin/php -f /Applications/MAMP/htdocs/a.php',$b);

then it says 127 instead of 5, I guess these are error codes

barredo
A: 

You obviously don't understand how system function works. What you really need is probably popen. Start the process with popen and then read its output with fgets for example.

Alternatively, you can use the backtick operator as already suggested by others.

Milan Babuškov
A: 

Try:

$output = array();
$returnCode = 0;
exec('/usr/bin/php -f /Applications/MAMP/htdocs/a.php 2>&1', $output, $returnCode);
print_r($output);

The 2>&1 redirects stderr to stdout, so any error messages will be captured in $output

Greg
A: 

Hang on... What are you actually trying to achieve here? Just run a.php and return the output to the browser? In that case, include it. From the looks of your posted content of a.php ( <?php echo 'hello world'; ?> ), that seems to be what you want.

If there's more to the content of a.php, then please post back and explain what you really want to do.

ZombieSheep
A: 

Try:

$fp = popen('/usr/bin/php -f file.php', 'r');

if(false === $fp)
{
   // something bad happened: error handle
}

$contents = '';

while(false === feof($fp))
{
  $contents .= fgets($fp);
}

fclose($fp);    
echo $contents;

That will allow you to capture the subshell's output and trap for errors.

Gary Richardson
A: 

}} For 'doesn't work' I meant: it does not return or print anything.

Try get a terminal/shell on your server and try your system command. If your OS is unixy you can use something like which php to find the correct path to the php cli.

You did install the php cli right? It generally isn't installed by default.

Does your web server do a chroot or something? Perhaps the php cli doesn't exist in the environment where you are trying to use system().

Instead of trying to call a script try doing a simple system('php -v'). Once you get that to correctly output the php version number then add the call to your script.

Zoredache