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

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

报告相同问题?

悬赏问题

  • ¥15 请分析一下这个电路设计的优点🙏
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 怎么改成输入一个要删除的数后现实剩余的数再输入一个删除的数再现实剩余的数用yes表示继续no结束程序
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取