Solved

Running an .exe file on a server with PHP

Posted on 2012-04-13
13
399 Views
Last Modified: 2012-05-07
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!
0
Comment
Question by:tcra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +2
13 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 37845215
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
 

Author Comment

by:tcra
ID: 37845216
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
 
LVL 2

Expert Comment

by:mdeweerd
ID: 37845224
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
ID: 37845227
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
 
LVL 86

Expert Comment

by:jkr
ID: 37845230
>>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
 

Author Comment

by:tcra
ID: 37845285
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
 
LVL 86

Expert Comment

by:jkr
ID: 37845296
Don't think there will be any issues once the program is compiled natively.
0
 
LVL 30

Expert Comment

by:Axter
ID: 37846291
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
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 37848246
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
 

Author Comment

by:tcra
ID: 37866512
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37867639
I believe that PHP can create any kind of file you want ;-)
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37936477
@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
 

Author Comment

by:tcra
ID: 37936488
I'll bear that in mind in the future.  Thanks for your good answer.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question