Solved

Running an .exe file on a server with PHP

Posted on 2012-04-13
13
395 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
  • 4
  • 4
  • 3
  • +2
13 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 86

Expert Comment

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

Expert Comment

by:Axter
Comment Utility
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 108

Accepted Solution

by:
Ray Paseur earned 250 total points
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
I believe that PHP can create any kind of file you want ;-)
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
@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
Comment Utility
I'll bear that in mind in the future.  Thanks for your good answer.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now