如何从php中的可执行文件中获取输出



我无法通过php从可执行文件(a.out(获得输出。我的php代码是:

$file = "/path/main.cpp";
$command="cd $path && c++ -lm ".$file;
exec($command);
$output = exec("cd $path && ./a.out");
exec("cd $path && ./a.out > res.txt");
var_dump($output);

所有的命令都被执行了,但是,我得到了空值;

exec("cd $path && ./a.out > res.txt");

这个命令也执行了,res.txt是在文件夹中创建的,但文件为空。如何解决这个问题?我的操作系统是CentOS 7

为了用PHP获得命令的输出,可以使用shell_exec函数:

shell_exec

shell_exec—通过shell执行命令并返回完整的输出作为字符串

所以,试试这个:

$output = shell_exec("cd $path && ./a.out");

最新更新