views:

114

answers:

5

Stupid question, this code:

<?php
$variable = system("cat /home/maxor/test.txt");
echo $variable;
?>

with file test.txt:

blah

prints:

blah
blah

What can I do with system() function to not print nothing so I get 1 "blah"???

+2  A: 

Use exec instead of system

http://us.php.net/manual/en/function.system.php#94262

Chacha102
+1  A: 

According to the manual -- see system() :

system() is just like the C version of the function in that it executes the given command and outputs the result.

Which explains the first blah


And :

Returns the last line of the command output on success

And you are echoing the returned value -- which explains the second blah.


If you want to execute a command, and get the full output to a variable, you should take a look at exec, or shell_exec.

The first one will get you all the lines of the output to an array (see the second paramater) ; and the second one will get you the full output as a string.

Pascal MARTIN
A: 

system is calling the actual cat program, which is outputting "blah" from test.txt. It also returns the value back to $variable which you're then printing out again.

Use exec or shell_exec instead of system.

Vivin Paliath
+1  A: 

system displays whatever the program outputs and returns the last line of output.

exec displays nothing and returns the last line of output.

passthru displays whatever the program outputs and returns nothing.

R. Bemrose
A: 

Use exec instead. To get all the output, rather than just the last line do this:

$variable = array();
$lastline = exec("cat /home/maxor/test.txt", $variable);
echo implode("\n", $variable);
thetaiko