Compile C File using PHP


I am currently using PHP on windows. I use a wamp server(Easy PHP) and codeblocks.
I already set the path from environment variable to compile a c file on cmd.
I use this command
gcc test.c -O3 -o test.exe
It can produce the test.exe

Now I want to execute it from a php file. I want the user to be able to compile their c file from a submit button.

print('<form action="compiler.php" method="POST"><input type="submit" value="Compile" ></form>');

Inside the compiler.php

<?php $output = system("C:\\Program Files\\CodeBlocks\\MinGW\\bin\\gcc.exe C:\\Program Files\\EasyPHP-5.3.9\\www\\upload\\test.c -O3 -o C:\\Program Files\\EasyPHP-5.3.9\\www\\upload\\test.exe"); echo $output; ?>

The result is an empty screen and no exe file produced in the ‘upload’ folder.

I already try
changing the $output variable to

$output = system('C:\Program Files\CodeBlocks\MinGW\bin\gcc.exe C:\Program Files\EasyPHP-5.3.9\www\upload\test.c -O3 -o C:\Program Files\EasyPHP-5.3.9\www\upload\test.exe");


$output = system("C:/Program Files/CodeBlocks/MinGW/bin/gcc.exe C:/Program Files/EasyPHP-5.3.9/www/upload/test.c -O3 -o C:/Program Files/EasyPHP-5.3.9/www/upload/test.exe");

But there is no result.
What am I missing?


LFNS :slight_smile: Long FileName Support

you have to explicity tell what is a directory/file in windows


$output = system('"C:\Program Files\CodeBlocks\MinGW\\bin\gcc.exe" "C:\Program Files\EasyPHP-5.3.9\www\upload\test.c -O3 -o "C:\Program Files\EasyPHP-5.3.9\www\upload\test.exe"'); echo $output;

Something like if you look at the properties of a shortcut icon/link in windows


Thanks. It works great. However, if I want to replace the source file and destination using variable. Why is not working?
I have this for the input

An the process after some parameter
echo $source;
$outputname=substr_replace($_FILES[‘uploadedfile’][‘name’], ‘exe’, strpos($_FILES[‘uploadedfile’][‘name’], ‘.’)+1);
echo $destination;
$output = system(’“C:\Program Files\CodeBlocks\MinGW\bin\gcc.exe” $source -O3 -o $destination’);

I want to make the exe file name is the same as the uploaded file name which is contained inside $_FILES[‘uploadedfile’][‘name’]