授予执行.c文件的权限

时间:2015-06-27 07:38:35

标签: php ubuntu permissions exec

我通过以下代码

创建了一个文件
$dir = '/home/srikanth/Desktop';
 if ( !file_exists($dir) ) {
  mkdir ($dir, 0777);
 }
//$code contains the code C code which i want to execute
 file_put_contents ($dir.'/test.c', $code);
$path = "/home/srikanth/Desktop/test.c";
$Command = "gcc $path 2&>1";
exec($Command,$return_val,$error);

当我打开我的错误日志文件  我看到以下错误

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

我尝试了以下命令来更改权限,我目前正在使用Ubuntu 14.04.2和Apache服务器

chmod 0777 /home/srikanth/Desktop/test.c
sudo chmod -R 777 /home/srikanth/Desktop/test.c




sudo usermod -aG www-data root
addgroup www-data

我使用这些命令将www-data添加到根组 但我仍然在我的错误日志中继续获取相同的文件

1 个答案:

答案 0 :(得分:0)

如评论中所述,此问题是权限问题,因为服务器用户无法在/ home / srinkanth / Desktop目录中创建文件夹/文件。或者gcc编译器中的某些内容在编译c代码时无法创建文件。

将777添加到该DIRECTORY(不是自己的测试文件)可能会解决问题。但是,良好的做法是在代码中添加退出/异常,以便在问题发生时及其发生的原因时为您提供更多可见性。

以下是我的建议:

$dir = '/home/srikanth/Desktop';
if (!is_dir($dir)) {
    $mkDir = mkdir($dir, 0777);
    if (!$mkDir) {
        exit('Failed to create ' . $dir);
    }
}

然后,您可以在put文件代码中添加额外的调试信息:

$codePath = $dir.'/test.c';
$created = file_put_contents($codePath, $code);
if (!$created) {
    exit('Failed to create ' . $codePath);
}

如果你的执行文件代码更多地进入这个阶段:

$codePath = $dir . '/test.c';
$command = "gcc $codePath 2&>1";

$output = [];
$returnStatus = '';
$lastLine = exec($command, $output, $returnStatus);

然后你可以输出你的整个gcc编译,通过将输出数组片段与新行粘合在一起,因为exec将每行输出解析为输出数组中的记录:

echo implode("\n", $output);

希望这会有所帮助。 古德勒克。

相关问题