Running an .exe file on a server with PHP

Hi guys,

I have an .exe file written in C/C++ that I need to be able to run on my web server.  When this .exe is run from the run line on my computer, it receives an input like this:

filename.exe -oTest.txt

and the .exe then simply creates a file named Test.txt in the same folder as the .exe.

I need to replicate this behaviour on my web server (Linux 64-bit), so I've placed the .exe in a folder (folder2) and enabled all permissions for this folder, and I'm trying to call the file in PHP using exec() as follows:

exec("/usr/bin/php5 folder2/filename.exe -oTest.txt > outputfile.txt");

When I run this PHP file, I'm hoping for the .exe to create a file named Test.txt in folder2 on the server, but at the moment it's not.  The file outputfile.txt is being created in the PHP file's folder, and it reads as follows:

Warning: Unexpected character in input:  '' (ASCII=5) state=1 in /home/websitename/public_html/folder1/folder2/filename.exe on line 704

Parse error: syntax error, unexpected T_STRING in /home/websitename/public_html/folder1/folder2/filename.exe on line 704

Is what I'm trying to do possible, and if so, am I way off track with the above?

Thanks in advance!
tcraAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
Well, first of all, you cannot run a Windows executable file on a Linux machine. If you however recompile it from the original sources, everything should work fine.
0
tcraAuthor Commented:
Ok, thanks a lot!  I'm not doing the C++ coding so have no idea about that side of things, but how much work would it be to recompile it?
0
mdeweerdCommented:
You can run certain windows executeables on Linux by using Wine.

The work involved in recompiling depends on the libraries that were used to build it.  It looks like a basic text processing program, so I guess the effort would be 'limited'.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

jkrCommented:
Depending on the complexity of the code, it can either be quite simple or require to create a makefile, especially of the source is still in flux and changed quite often. An easy solution in between (if the code is 'static') would be to create a script to compile it, so you don't have to type a lengthy command line everytime you do that. It would look like

#!/sbin/bash
g++ -o programname file1.cpp file2.cpp file3.cpp [...] [-I/path/to/headers/if/required] 

Open in new window


if it's just one single .cpp file, thigs become easy:

g++ -o programname file.cpp

Open in new window

0
jkrCommented:
>>You can run certain windows executeables on Linux by using Wine.

Have you thought about the overhead of starting Wine in a web server environment? Multiple times? For each user request?
0
tcraAuthor Commented:
Thanks a lot for the assistance guys.  I'll report back once I've heard from the C++ coder.  Do you envisage that I wouldn't have any problems running the program on the server and having it create files once this is resolved?
0
jkrCommented:
Don't think there will be any issues once the program is compiled natively.
0
AxterCommented:
If the C++ code is not that complex, you might be better off converting the C++ code to PHP.

Running executables from PHP can be problematic if you don't have the correct file permissions or PHP configuration.
0
Ray PaseurCommented:
simply creates a file named Test.txt in the same folder
I have a Linux server.  When I want to create a file with PHP I can do something like this.  The only moving part is line 18 - the rest of it is just demonstration code to prove that it works.
<?php // RAY_temp_tcra.php
error_reporting(E_ALL);

// CHOOSE THE PATH FOR THE FILE WE WANT TO CREATE
// WITH PROPER AUTHENTICATION, THIS COULD BE ACCEPTED FROM
// AN EXTERNAL SOURCE SUCH AS A GET-METHOD REQUEST
$path = 'RAY_junk/RAY_temp_tcra.txt';

// MAKE SURE WE ARE STARTING FRESH
unlink($path);

// DOES THE FILE EXIST?
clearStatCache();
if ( is_file($path)) echo "<br/>EXISTS: $path";
if (!is_file($path)) echo "<br/>DOES NOT EXIST: $path";

// CREATE THE FILE
file_put_contents($path, NULL);

// DOES THE FILE EXIST?
clearStatCache();
if ( is_file($path)) echo "<br/>EXISTS: $path";
if (!is_file($path)) echo "<br/>DOES NOT EXIST: $path";

Open in new window

This may give you a faster path to success than trying to get a C++ script to run as an executable.

HTH, ~Ray
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tcraAuthor Commented:
Hi Ray,

Thanks for your input.  I think I may go the route of trying to create the file using PHP.  The .exe I discussed above was just a test to see if this kind of thing would work, but the actual program will be creating .BIN files.  Is this still possible in PHP?
0
Ray PaseurCommented:
I believe that PHP can create any kind of file you want ;-)
0
Ray PaseurCommented:
@tcra: Please see the grading guidelines.
http://www.experts-exchange.com/help/viewHelpPage.jsp?helpPageID=26

What was wrong with the answers?  What part was missing or not explained?
0
tcraAuthor Commented:
I'll bear that in mind in the future.  Thanks for your good answer.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.