Web Development

Fails to open file on file_get_contents() – PHP – SitePoint Forums


Hello, can someone help me clean this code? It shows no output and fails to open file on file_get_contents(). Thank you in advance!

for index.php

<!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>Java Compiler</title>
            </head>
        <body>
            <form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ; ?>" >
                <label>Select Language :</label>
                <select name="language" style="margin-bottom: 10px;margin-top: 10px; ">
                    <!--<option value="cpp">C++</option>
                    <option value="c">C</option>-->
                    <option value="java">JAVA</option>
                </select></br>
                <textarea placeholder="// Enter your code here. (* The Class Name MUST Be code *)" name="code" rows="16" cols="80"></textarea>
                </br></br>
                <label style="margin-bottom: 10px;margin-top: 10px; ">Input : </label></br></br>
                <textarea placeholder="// Enter your inputs here." name="input" rows="10" cols="80"></textarea>
                </br>
                </br><input type="submit" name="run" value="Run">
            </form>
        </body>
    </html>
    <?php
        if ($_SERVER['REQUEST_METHOD'] == 'POST')
        {
            if ($_POST['language']=='java'){
                include("java.php");
            }
        }
        
            
    ?>

for java.php

 <?php
        $output             = "";
        putenv("PATH=C:\Program Files\Java\jdk-21\bin");
        //shell_exec("javac code.java 2>&1");
        //echo shell_exec("java code 2>&1");
        
        // Variables 
        $cc                 = "javac";                       // CMD Command for Compiling the code file 
        $out                = "java code";                   // CMD Command for executing the code file 
        $code               = $_POST['code'];                // Getting the value of the code field
        $input              = $_POST['input'];               // Getting the values of the inputs field
        $code_path          = "code.java";                   // The path of the code file for compileing 
        $input_file         = "input.txt";                   // The path of the input file 
        $error_file         = "error.txt";                   // The path of the error file 
        $runtime_file       = "runtime.txt";                 // The path of the runtime errors file ( Stores the runtime errors in the program )
        $executable         = "*.class";
        $command            = $cc . " " .$code_path;        // CMD Compile permission 
        $command_error      = $command . " 2>".$error_file; // CMD Getting Errors Permission 
        $runtime_error_file = $out. " 2>".$runtime_file;      // CMD Getting Runtime Erros Permission 
        
		$error_file_open   = fopen($error_file,'w+') or die("Unable to open file!");
        $file_code         = fopen($code_path,'w+') or die("Unable to open file!");
        fwrite($file_code,$code);
        fclose($file_code);
        $file_in           = fopen($input_file,'w+') or die("Unable to open file!");
        fwrite($file_in,$input);
        fclose($file_in);
        
        exec("cacls  $executable /g everyone:f"); 
    	exec("cacls  $error_file /g everyone:f");
        
        // compiling the file
        shell_exec($command);
        $path = __DIR__ . 'error.txt';
        $error               = file_get_contents($error_file);
        echo $error;

        // the conditions
        if (trim($error)=="")
        {
            if (trim($input)=="")
            {
                shell_exec($runtime_error_file);
                $runtime_error= file_get_contents($runtime_file);
                $output       = shell_exec($out);
            }else{
                shell_exec($runtime_error_file);
                $runtime_error= file_get_contents($runtime_file);
                $out          = $out . " < " .$input_file;
                $output       = shell_exec($out);
            }
    ?>
            <label style="margin-bottom: 10px;margin-top: 10px; ">Output : </label></br></br>
            <textarea placeholder="// The result is :"  rows="10" cols="80"><?php echo $output; ?></textarea>
            <?php
            }else if (!strpos($error,"error"))
            {
            if (trim($input) == "")
            {
                $output=shell_exec($out);
            }else{
                $out=$out." < ".$input_file;
			    $output=shell_exec($out);
            }
            ?>
            <label style="margin-bottom: 10px;margin-top: 10px; ">Output : </label></br></br>
            <textarea placeholder="// The result is :"  rows="10" cols="80"><?php echo $output; ?></textarea>
            <?php
        }else
            {
            ?>
                <label style="margin-bottom: 10px;margin-top: 10px; ">Output : </label></br></br>
                <textarea placeholder="// The result is :"  rows="10" cols="80"><?php echo "<pre>$error</pre>"; ?></textarea>
            <?php
                
            }
            exec("del $code_path");
			exec("del *.txt");
			exec("del $executable");
            ?>

You haven’t written anything to $error_file. So what are you expecting to read?



Source

Related Articles

Back to top button