如何在php中从可执行文件获取输出
我无法通过php从可执行文件(a.out)获得输出。 我的php代码是:如何在php中从可执行文件获取输出,php,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); 所有的命令都被
$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执行命令并返回完整的输出 一串 那么,试试这个:
$output = shell_exec("cd $path && ./a.out");
似乎是您想要的。关于重定向中的“空文件”,您确定程序确实生成了任何输出吗?或者编译成功并且
a.out
程序存在?是的,我从终端检查,没问题,但是通过php no outputTry捕获错误输出,查看是否失败()。我得到这个:sh:./a.out:Permission deniedsh:./a.out:Permissiondenied@BakhodirIsmatov所以问题是另一个。。。当您解决它时,shell_-exec
将执行您需要的操作。apache可以运行a.out吗?@BakhodirIsmatov尝试从终端运行它(如果可以的话),否则尝试在shell_-exec
中运行它:echo shell_-exec('echo“it works”)代码>。因为我不知道a.out
包含什么,所以在这方面我帮不了你。