display ghostscript version number through PHP in ubuntu

寵の児 提交于 2019-12-23 23:42:44

问题


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.


回答1:


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




回答2:


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
"


来源:https://stackoverflow.com/questions/6626995/display-ghostscript-version-number-through-php-in-ubuntu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!