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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵