display ghostscript version number through PHP in ubuntu
I have written the following code for showing the version number of ghostscript:
<html>
<head>
&开发者_开发百科lt;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.
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
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
"
精彩评论