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 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?