duanjuda5789 2015-06-27 07:38
浏览 74

授予执行.c文件的权限

i created a file by the following code

$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);

when i open my error log file i see the following error

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

i tried the following commands to change the permission ,I am currently using Ubuntu 14.04.2 and Apache server

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

I used these commands to add www-data to root group but i still keep on getting the same file in my error log

  • 写回答

1条回答 默认 最新

  • douluokuang7184 2015-06-27 08:06
    关注

    As mentioned in the comments, this issue is a permissions issue, in so much as the server user can not create folders/files in the /home/srinkanth/Desktop directory. Or something in the gcc compiler can not create files during compilation of your c code.

    Adding 777 to that DIRECTORY (not the test file on its own) will likely fix the problem. However, good practice is to add exits/exceptions in your code to give you more visibility on problems if and when they happen and what caused them.

    Here is my advice:

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

    Then you can add additional de-bugging info to your put file code:

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

    The if you reach this stage more to your execute file code:

    $codePath = $dir . '/test.c';
    $command = "gcc $codePath 2&>1";
    
    $output = [];
    $returnStatus = '';
    $lastLine = exec($command, $output, $returnStatus);
    

    Then you can output your whole gcc compile by glueing the output array pieces back together with a new line as exec parses each line of output as a record in the output array:

    echo implode("
    ", $output);
    

    Hope this helps. Goodluck.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看