Compile C File using PHP


#1

Hi,
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]

<?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; ?>

[/php]
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");

and

$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?


#2

LFNS :slight_smile: Long FileName Support

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

try

$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


#3

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
[php]echo"";[/php]

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

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