2

I have written the following code for showing the version number of ghostscript:

<html>
<head>
<title></title>
</head>
<body>
<?

$ver = shell_exec("/usr/bin/gs --version");
//$ver = exec(GS_BIN . " --version");
print "$ver";
print "A";

?>
</body>
</html>

I can get the A printed, but not the version number why?

Thanks.

3
  • Try replacing shell_exec with exec. Commented Jul 8, 2011 at 15:53
  • @hakre: Why are you asking me? Commented Jul 8, 2011 at 16:44
  • @lokheart: If you execute that command manually in the shell, what does it print on screen? @Rocket: sorry. Commented Jul 8, 2011 at 19:02

2 Answers 2

2

Possibly ghostscrsipt is writing the data out to STDERR instead of STDOUT. Try doing

/usr/bin/gs --version 2>&1 

to redirect stderr to stdout and try again

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

4 Comments

I need to try the gs version number because I need to use php to use gs to capture some image, but I can't run it using exec, so I try a smaller task of showing the version number, but still fail
@lokheart: Have you tried to redirect STDERR to STDOUT like Marc B suggested?
i tried $ver = shell_exec('/usr/bin/gs --version 2>&1'); and the output is string(111) "/usr/bin/gs: /opt/lampp/lib/libgcc_s.so.1: version GCC_4.2.0' not found (required by /usr/lib/libstdc++.so.6) "`, what has gone wrong?
If gs works from the command line, then whatever shell is being invoked by PHP doesn't have the proper library path, and can't find the gcc library that GS was compiled against.
1

You should use var_dump($ver); for debugging purposes, because your code just works:

$ php -r "echo shell_exec('/usr/bin/gs --version');"
8.71

I just had it run on my linux box and according to shell_exec() Docs, it should be fine.

Things to look for:

  • Safe Mode enabled?
  • exec() can return the exit code / return status of the command.
  • if it returns NULL, see this answer.

STDERR and shell_exec()

shell_exec() will only return the commands output written to STDOUT. In case the command can not be invoked by the shell, this function will return NULL and it will hide away what has been reported as error.

To include errors as well in the return value, STDERR needs to be redirect to STDOUT. This is done by adding 2>&1 to the end of the command. Here is the same example code with a wrong command for demonstration:

$ php -r "var_dump(shell_exec('/usr/bin/gs2 --version 2>&1'));"
string(44) "sh: /usr/bin/gs2: No such file or directory
"

2 Comments

and I have checked phpinfo(), safe mode is disabled
I added a reference to the question. Check if it helps.

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.