0

I want to see the output of the following code in the web browser:

code:

<?php

      $var = system('fdisk -l');

      echo "$var";
?>

When I open this from a web browser there is no output in the web browser. So how can I do this? Please help!

Thanks Puspa

1
  • 1
    Do you get a warning regarding safe mode maybe? Commented Jun 15, 2010 at 8:21

3 Answers 3

3

you can use passthru, like so:

$somevar = passthru('echo "Testing1"');
// $somevar now == "Testing1"

or

echo passthru('echo "Testing2"');
// outputs "Testing2"
Sign up to request clarification or add additional context in comments.

1 Comment

Although, if php doesn't have privileges to execute the command, you'll have trouble. In the case that you think you might be running into these issues, redirect stdout & stderr to a log file, and check it after you try. You can send these to a log by appending this to the end of any shell command: >>logfile.log 2>&1
1

use exec('command', $output);

print_r($output);

Comments

1

First of all, be sure that you (=user under which php runs) are allowed to call the external program (OS access rights, safe_mode setting in php.ini). Then you have quite a few options in PHP to call programs via command line. The most common I use are:


system

This function returns false if the command failed or the last line of the returned output.

$lastLine = system('...');

shell_exec or backtick operators

This function/operator return the whole output of the command as a string.

$output = shell_exec('...'); // or:
$output = `...`;

exec

This function returns the last line of the output of the command. But you can give it a second argument that then contains all lines from the command output.

$lastLine = exec('...'); // or capturing all lines from output:
$lastLine = exec('...', $allLines);

Here is the overview of all functions for these usecases: http://de.php.net/manual/en/ref.exec.php

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.