从php执行C代码

波基里

我正在尝试使用php编译并运行本地存储在计算机中的C文件,我使用了以下代码

$dir = '/home/User/Desktop';
 if ( !file_exists($dir) ) {
  mkdir ($dir, 0744);
 }

 file_put_contents ($dir.'/test.c', $code);

$path = "/home/User/Desktop/test.c";
$Command = "gcc $path 2&>1";
exec($Command,$return_val,$error);

我已经使用chmod和chgrp设置了文件的所有权限,但是在执行该文件时,它在屏幕上回显了“成功”,但未显示$ output值。这是我输入的示例C程序

#include <stdio.h>
int main()
{
printf("Hello world");

return 0;

}

但是当我使用GCC执行以下程序时,该程序运行良好,我目前正在使用Ubuntu 14.04.2 LTS

我尝试使用

$Command = "gcc $path 2&>1"; $output = exec($Command); $output = exec(./a.out);

但我仍然无法获得想要的输出

我尝试使用

$output = exec($Command,$return_val,$error);

现在,当我回显$ error时,它给了我“ 2”

我尝试使用系统

$last_line = system('gcc $path', $retval);
echo '
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;

我得到以下输出

Last line of the output: 
Return value: 4

sh: 1: cannot create 1: Permission denied
gcc: error: 2: No such file or directory

但是我使用了该文件的权限

 sudo chmod g+w /home/User/Desktop/test.c

谁能帮我解决这个问题?

耻辱

不是2&>1,是2>&1

sh: 1: cannot create 1: Permission denied
gcc: error: 2: No such file or directory

您在这里看到的第一个错误是Shell抱怨无法创建名为“ 1”的文件来重定向stdout。
第二个错误是gcc抱怨缺少名为“ 2”的输入文件。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章