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().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择