简体   繁体   English

为什么PHP的shell_exec没有给出一些a.out文件的输出

[英]Why shell_exec of php is not giving output of some a.out file

I am working with a php script that can compile and run c++ code . 我正在使用可以编译和运行c ++代码的php脚本。 maximum kinds of c++ code is being compiled and giving output by shell_exec command. shell_exec命令正在编译最大数量的c ++代码并提供输出。 But some kinds of c++ code having structure, class are being compiled but are unable to fetch output from a.out file by shell_exec in apache server. 但是正在编译具有结构,类的某些c ++代码,但无法通过apache服务器中的shell_exec从a.out文件中获取输出。 As a result it's output is showing null in php. 结果,它的输出在php中显示为null。 It is noted that in ubuntu terminal it works but when i run it from php it can not be run. 请注意,在ubuntu终端中它可以工作,但是当我从php运行它时无法运行。 i am running it on ubuntu, apache server. 我正在Ubuntu服务器上运行它。 Here is my php code that can run c++ code 这是我的可以运行C ++代码的PHP代码

<?php
$CC="g++";
$out="timeout 5s ./a.out";
$code=$_POST["code"];
$input=$_POST["input"];
$filename_code="main.cpp";
$filename_in="input.txt";
$filename_error="error.txt";
$executable="a.out";
$command=$CC." -lm ".$filename_code;    
$command_error=$command." 2>".$filename_error;
$check=0;

//if(trim($code)=="")
//die("The code area is empty");

$file_code=fopen($filename_code,"w+");
fwrite($file_code,$code);
fclose($file_code);
$file_in=fopen($filename_in,"w+");
fwrite($file_in,$input);
fclose($file_in);
exec("chmod -R 777 $filename_in");
exec("chmod -R 777 $filename_code");  
exec("chmod 777 $filename_error");  

shell_exec($command_error);
exec("chmod -R 777 $executable");
$error=file_get_contents($filename_error);
$executionStartTime = microtime(true);

if(trim($error)=="")
{
    if(trim($input)=="")
    {
        $output=shell_exec($out);
    }
    else
    {
        $out=$out." < ".$filename_in;
        $output=shell_exec($out);

    }
    //echo "<pre>$output</pre>";
          echo "<textarea id='div' class=\"form-control\" name=\"output\" rows=\"10\" cols=\"50\">$output</textarea><br><br>";
}
else if(!strpos($error,"error"))
{
    echo "<pre>$error</pre>";
    if(trim($input)=="")
    {
        $output=shell_exec($out);
    }
    else
    {
        $out=$out." < ".$filename_in;
        $output=shell_exec($out);
    }
                    echo "<textarea id='div' class=\"form-control\" name=\"output\" rows=\"10\" cols=\"50\">$output</textarea><br><br>";
}
else
{
    echo "<pre>$error</pre>";
    $check=1;
}
$executionEndTime = microtime(true);
$seconds = $executionEndTime - $executionStartTime;
$seconds = sprintf('%0.2f', $seconds);
echo "<pre>Compiled And Executed In: $seconds s</pre>";


if($check==1)
{
    echo "<pre>Verdict : CE</pre>";
}
else if($check==0 && $seconds>3)
{
    echo "<pre>Verdict : TLE</pre>";
}
else if(trim($output)=="")
{
    echo "<pre>Verdict : WA</pre>";
}
else if($check==0)
{
    echo "<pre>Verdict : AC</pre>";
}

exec("rm $filename_code");
exec("rm *.o");
exec("rm *.txt");
exec("rm $executable");
?>

While running through terminal, you are executing PHP script with your user permission. 通过终端运行时,您正在以用户权限执行PHP脚本。 But while running the script using apache, you are running the script under www-data user. 但是,在使用apache运行脚本时,您是在www-data用户下运行脚本。 Some executable can not be run under www-data user. 某些可执行文件无法在www-data用户下运行。 A solution that you can try is to add www-data to your user group. 您可以尝试的解决方案是将www-data添加到您的用户组。

You can find an interesting read at https://unix.stackexchange.com/a/127529 您可以在https://unix.stackexchange.com/a/127529上找到有趣的读物

At last i have found my solution. 最后,我找到了解决方案。 i have just changed my g++ version to 4.9 and then shell_exec() has started working. 我刚刚将我的g ++版本更改为4.9,然后shell_exec()开始工作。

Install g++ 4.9 安装g ++ 4.9

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install g++-4.9

Set g++ 4.9 设置g ++ 4.9

sudo ln -f -s /usr/bin/g++-4.9 /usr/bin/g++

The c++ structure and class codes that were not being able to show output previously are showing output now. 以前无法显示输出的c ++结构和类代码现在正在显示输出。 I don't know why in terminal the g++ 5 version was working but not working in shell_exec() but g++ 4.9 is working in both terminal and shell_exec() 我不知道为什么终端机中的g ++ 5版本可以运行,但不能在shell_exec()中运行,但是g ++ 4.9在终端机和shell_exec()中都可以运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM