duanpai1033
2018-09-05 12:16
浏览 136
已采纳

为什么有些二进制文件不能与shell_exec一起使用?

I don't understand why, but I can't execute some binaries from a PHP page.

If I call this from a PHP page, I don't get any output:

<?php
echo shell_exec('/usr/bin/which ffmpeg');

If I execute it from CLI, it works:

$ sudo -u apache php -r 'echo shell_exec("/usr/bin/which ffmpeg");'
/usr/bin/ffmpeg

But if I try to call whereis instead of which from a PHP page, I do get an output although they are in the same directory:

<?php
echo shell_exec('/usr/bin/whereis ffmpeg');

I don't understand the logic behind...


I've verified the file permissions, and everything seems fine: -rwxr-xr-x. root:root, and there's is no ACL.

I'm using Fedora 28 (SELinux is set to permissive). These commands work on Debian and Ubuntu.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanlan3598 2018-09-05 13:15
    已采纳

    The which command will generate the output on STDOUT, when the program is found, but it will use STDERR when there is no such command.

    $ strace which php
    [...]
    access("/usr/bin/php", R_OK)            = 0
    fstat(1, {st_mode=S_IFCHR|0600, st_rdev=makedev(136, 0), ...}) = 0
    write(1, "/usr/bin/php
    ", 13) = 13
    [...]
    

    Notice the 1 at write(), which is the handler for STDOUT, but when the command is not found, a different handler is used:

    $ strace which something_not_existing
    [...]
    stat("/usr/games/bin/something_not_existing", 0x7ffc20f046a0) = -1 ENOENT (No such file or directory)
    write(2, "which: no something_not_existing"..., 150) = 150
    [...]
    

    Notice the 2 at write(), which is the handler for STDERR.

    In the console, you will see both the output from STDOUT and the output from STDERR, but shell_exec() will only output/return the STDOUT stream.

    Check http://www.php.net/shell_exec on how to capture the STDERR stream as well. You might even want to use other execution functions like proc_open() to control the STDIN/STDOUT/STDERR streams, depending on your requirement.

    The whereis command will always write to STDOUT, thats because you see the output in php via shell_exec().

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题