无法通过php exec运行shell命令,但可以作为shell上的用户吗?

Ora*_*555 4 linux bash shell perl exiftool

我正试图让exiftool在我的专用服务器上工作.问题是PHP exec似乎与命令以用户身份运行时不同.奇怪的是,PHP显示为我登录的同一用户,但它与系统命令的行为不同.

奇怪的是,在我的本地主机上一切都很好,但在我的服务器上却不行.

如上所述,通过ssh登录的exiftool命令运行正常.

但是在php测试脚本中运行(注意我已经在每个测试目录上安装了exiftool,并且它通过ssh运行),没有任何东西可以访问,尽管它以用户orangeman运行...

它失败了

这是一个更新 - 整天都在这里:

在shell上:

-bash-4.1$ which exiftool -a
~/perl5/bin/exiftool
/usr/bin/exiftool
~/perl5/bin/exiftool
Run Code Online (Sandbox Code Playgroud)

在PHP中 shell_exec('exiftool -a');

/usr/bin/exiftool
Run Code Online (Sandbox Code Playgroud)

以下是该文件链接到的内容:

lrwxrwxrwx    1 root root          33 May 15 02:10 exiftool -> /home/orangeman/perl5/bin/exiftool
Run Code Online (Sandbox Code Playgroud)

我也试过创建各种各样的符号链接,通过putenv();php 篡改主要的$ PATH变量......我真的在黑暗中.适用于localhost,而不是专用服务器.


我用赏金更新了这个 - 这是一个严重的问题.

我在专用服务器上,问题如上所述.


UPDATE Per @gcb建议,我能够打印出php的exec()函数运行系统命令时发生的错误,但没有效果.

PHP

<?php
exec('exiftool 2>&1', $output, $r);
var_dump($output, $r);
?>
Run Code Online (Sandbox Code Playgroud)

输出:

array(2) {
  [0]=>
  string(230) "Can't locate Image/ExifTool.pm in @INC (@INC contains: /bin/lib /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /bin/exiftool line 33."
  [1]=>
  string(59) "BEGIN failed--compilation aborted at /bin/exiftool line 33."
}
Run Code Online (Sandbox Code Playgroud)

UPDATE

@ gcb的解决方案奏效了.非常感谢你.

gcb*_*gcb 5

所以你现在没有php问题,而是一个perl问题.你的包含路径很糟糕.

在这里回答http://u88.n24.queensu.ca/exiftool/forum/index.php?topic=2217.0

You either have to install the ExifTool libraries in the
standard location (ie. somewhere in the @INC directories
listed in your post), or add the location to the include path,
something like this:

Code:
#!/usr/bin/perl
BEGIN { unshift @INC, "PATH_TO_DIRECTORY_CONTAINING_LIBRARIES" }
use Image::ExifTool;

You should be able to add "Image/ExifTool.pm" to the path you add
to find the ExifTool module.

- Phil
Run Code Online (Sandbox Code Playgroud)

我仍然认为使用上一个答案的#3建议将解决它.如果没有,你真的想知道原因,创建一个新的perl脚本,只输出内容@INC并通过shell和php运行它.你会看到差异,然后你需要找到哪些登录脚本没有在php中被尊重并打开一个针对PHP的错误,因为shell_exec不尊重它...

虽然对您的问题更简单的解决方案(因为它看起来您对解释不太感兴趣)是在调用脚本之前设置PERLLIB var.

那么,就这样做:

  1. find / -name ExifTool.pm这将告诉您lib的安装位置.让我们说这回来了/example/perl/Image/ExifTool.pm
  2. 附加PERL5LIB=/example/perl/到你的exec()调用.
  3. exec("PERL5LIB =/example/perl/sudo /var/www/myscript/execscript.sh {$ param}");