1

shell_exec() is not working on my localhost. I read a lot of websites to resolve this issue but can't find a solution. This code:

    $output = shell_exec("env");
    print "<pre>$output</pre>";

Doesn't give any output.

I checked in php.ini disable_functions but shell_exec is not disabled. I did error_reporting = E_ALL but again no output (no error). All I get is just a blank screen. Even safe_mode is off.

If I write echo "BULB"; after the above code it is printing "BULB".

What could be the issue?

7
  • 1
    If you simply run env in your console, you get the output? Commented Oct 5, 2015 at 12:23
  • I am getting this output ReferenceError: env is not defined Commented Oct 5, 2015 at 12:25
  • Do other commands like ls or dir work? Commented Oct 5, 2015 at 12:35
  • 1
    That error message looks like JavaScript to me. What shell are you using? Commented Oct 5, 2015 at 12:39
  • 1
    Thinking about it, perhaps you misunderstood @Federico's comment and executed the command from the JS console in your browser...He meant to open up a command prompt / terminal emulator and run the command from there. In order for us to help you, we need to know more about your system. In particular, it would be useful to know the operating system and the default shell. Commented Oct 5, 2015 at 13:06

1 Answer 1

2

What info are you expecting to get from env? From your comments, it seems to me as if you're trying to use a Linux command on a Windows system -- that's never going to work.

On Linux systems, the env command on its own like that returns a list of environment variables that have been defined. However env is not a valid command in Windows.

If you're simply looking for the list of environment variables, this can actually be obtained in PHP without having to go to a shell command. PHP has a built-in global variable $_ENV which contains a copy of all the environment variables that were defined when the program stated. Simply print_r($_ENV) to see them.

On the other hand, if you really need to use shell_exec() for some reason, then you need to take into account the operating system that you're using. On Linux you would use env command. The equivalent on Windows is set without any arguments. So your code becomes:

$output = shell_exec("set");

Note that the format of the output may not be identical to what you'd get on Linux, so if you're parsing it, that code will have to change too.

If you need your code to be able to run on multiple platforms then you would need to write additional code before the shell_exec() call to determine the operating system and work out the correct command to use.

Sign up to request clarification or add additional context in comments.

4 Comments

In general, your point regarding portability of calls to shell_exec is valid but env is a valid command in Windows.
@TomFenech - I just opened my dos prompt and typed 'env', and got the classic 'env' is not recognised as an internal or external command.. So if it is a Windows command, it isn't in my system's path.
Fair enough - it's a valid command on my system (Windows 8.1) in the command prompt.
hmm, I'm also on Win 8.1, so I don't know what the difference is.

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.