gpt4 book ai didi

PHP exec 不返回特定程序的字符串

转载 作者:太空宇宙 更新时间:2023-11-03 23:34:12 25 4
gpt4 key购买 nike

如果我在 shell 中执行此命令,我可以看到返回字符串。然而,exec()没有返回任何东西。我也试过shell_exec()无济于事。 php.ini 中的安全模式已关闭。可执行文件的权限是 rwxr-x-r-x .我试过使用完整路径和短路径。我可以执行ls没有问题,它返回适当的数据,但由于某种原因我无法从我编写的这个 c 程序中获得输出。它打印到 stdout使用 printf()功能,并且再次毫无问题地打印到命令行,但 php 不会接收它。什么给了?

$string_array;<br/>
exec("/bin/weight",$string_array);<br/>
var_dump($string_array);

替换 "/bin/weight""/bin/ls"显示正确的结果。当我执行 "/bin/weight"在 shell 中,它打印 {motion=false,weight=0,uom="LBS"}正如它应该的那样,但它以某种方式没有使它成为 php。再次,在php.ini , safe_mode = Off .在这里拉我的头发...

c 程序的重要部分:

//build return string and send back to stdout
char weight_buf[32];
char result[1024]="{motion=";

if(motion==FALSE) {
strcat(result, "false,");
} else {
strcat(result, "true,");
}
strcat(result, "weight=");
if(negative==TRUE) {
strcat(result, "-");
}
//itoa(weight, weight_buf, 10);
sprintf(result, "%s%d", result, weight);
strcat(result, weight_buf);
strcat(result, ",uom=\"");
strcat(result, "LBS"); //TODO: fill in the actual units here
strcat(result, "\"}");
printf("%s\n", result);

return 0;

上面的代码打印到stdout从命令行执行时就好了:

最佳答案

来自 exec manual page :

If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable.

为了打印标准输出,您正在寻找的是 system()passthru()

需要说明的是,您可以将变量传递给 exec() 以接收所有输出,但此函数不返回它,仅返回脚本的返回码。

关于PHP exec 不返回特定程序的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8125935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com