Forum Moderators: coopster

Message Too Old, No Replies

PHP 'system' command

         

cath2010

9:26 pm on Apr 7, 2010 (gmt 0)

10+ Year Member



Dear all,

I would like to execute the PHP 'system' command (or 'exec' command) to run an executable file on a web page.

I am able to run the following command on my computer (local host) but not from the web. On my computer, the message is displayed. On the web, no error message is displayed but the expected message is not displayed.
<?php
system('TestWebPage.exe');
?>
The executable file simply writes a message on the web page:
#include <stdio.h>
void main ()
{
printf("Run C++ source code");
}

I have copied the executable file in the 'www' folder together with the PHP files. I assume that it is the correct place for this file. I made the following tests:
1) If I write system('') instead of system('TestWebPage.exe'), an error message is displayed because the system command is empty.
2) If I try to run an inexistant executable file, nothing happens (no error message). As this behaviour is similar to the one obtained when I run the correct executable file, I am not sure that the executable file is found.
3)The following command is working correctly both on localhost and on the web:
<?php
print system('dir');
?>

Do you have any idea about the problem?

Thanks in advance.

Demaestro

9:39 pm on Apr 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I am a little confused as to what you want to happen.

You want to run the executable on the client machine?

If yes ->
-- Do you want the user to be prompted to run or save the file?


Or is it that you want the executable to run on the server?

Matthew1980

9:51 pm on Apr 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there cath2010,

Welcome to the forum!

Looks like you are doing a "hello world" type of thing using php with C to output the contents of an exe file (which is a dangerous thing IMO)

You say that you have the file in the same directory as the web page, if so, instead of just asking for the file directly, check to see if it exists first before calling it:-

I am just using ./ to say from the current working directory
<?php
error_reporting(E_ALL);

if(file_exists("./TestWebPage.exe")){
//print system('./TestWebPage.exe');//You may need to 'print' the contents of the file
system('./TestWebPage.exe');
exit;
}else{
echo "No file there to run!";
exit;
}
?>

Try both methods there, as you may need to print the contents from the exe file, though I may be wrong, just uncomment/comment as necessary.

As for the issue with the web server, make sure that the hosting server allows you to run .exe files from your script, and may be a good idea to include the entire file path.

Also, for any issues that php may be flagging up, it may be wise to put error_reporting(E_ALL); right under the opening <?php just to see if there is anything going on.

Personally though, this may just be a path issue, locally you can tend to get away with using relative type urls, but on a server, it is best to use absolute urls so that you can guarantee the file is there to open.

Other than that, I'm not sure.

Have a read of this too, good tips to follow :)

[uk3.php.net ]

Hope that helps a little anyway,

[EDIT]Hi there Demaestro, I think as the aim is similar to using a dll to process data, though I could be wrong.

Cheers,
MRb

cath2010

9:01 pm on Apr 8, 2010 (gmt 0)

10+ Year Member



Dear Demaestro,

I would like to run the executable file on the server.

Cheers,

cath2010

9:15 pm on Apr 8, 2010 (gmt 0)

10+ Year Member



Hi Matthew1980,

I run the code that you sent me to check whether the file exists before calling it and the file is correctly found.
<?php
error_reporting(E_ALL);
if(file_exists("./TestWebPage.exe")){
//print system('./TestWebPage.exe');//You may need to 'print' the contents of the file
echo 'file exists';
print system('./TestWebPage.exe');
exit;
}else{
echo "No file there to run!";
exit;
}
?>

Nevertheless, with both "print system('./TestWebPage.exe');" and "system('./TestWebPage.exe')" commands, no output is displayed. The error_reporting() function does not displayed any error message.

I will check whether the hosting server (OVH) allows me to run .exe file.

As its my first experience on web page, I will explain you the aim of this test because the PHP 'system' function is maybe not the most appropriate way. My idea is to run a C++ program on the server and to display the output on the webpage. The C++ program would read some user input data stored in a MySQL database. Do you have any recommendation or reference(web link, book) for the devopment of this type of application?

Cheers,

Matthew1980

9:32 pm on Apr 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there cath2010,

To be honest, this seems to be a rather convoluted way of approaching something that php does for you anyway. As you are just using the .exe file to read a db, this could easily be done with the standard functions that are included in php.

As for literature on php/mysql check out amazon for a book by Welling & Thompson, that's just my personal choice, but for a web based reference (as well as here) php.net is about the best there is.

I kinda see what you are trying to do though, though this is usually achieved through a dll file, compiled using C I think, but that's outside the scope of my knowledge. I see lots of websites these days using similar methods, car insurance companies & comparison websites etc.

The only time I have seen an .exe in use from a web server on the wireless router/modem when you initially connect to your ISP.. though I think that may be dependant on the manufacturer.

Hope I have clarified some aspects for you :)

Cheers,
MRb

Demaestro

9:37 pm on Apr 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Something you can do to see if it is being executed, but maybe you are just missing the output, is put a logging line in the C function, something that writes a timestamp to a file or a db then you can check after to see if the file is executing.

You may have to use a .NET library for it to get it working. I know there are ways of interacting PHP with .NET stuff.

Matthew1980

9:47 pm on Apr 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Demaestro,

That could be the case, the exe could be executing quicker than can be seen, the old flashing LED scenario. I see where you are going with the time stamp though, nifty, I should have thought of that in the first place. Doh.

But I still think as the database retrieval can be done directly through php, no need to over engineer something.

Cheers,
MRb

cath2010

9:51 pm on Apr 8, 2010 (gmt 0)

10+ Year Member



Thank you for your answers.

I will look at your recommendations. The aim of the running of the executable file is not just to read the database. The application should read the data base and then process the data through mathematical functions before displaying the outputs. As I have already all the mathematical models programmed in C/C++, I would like to reuse this code.

Cheers,

eelixduppy

10:25 pm on Apr 8, 2010 (gmt 0)



>> I am able to run the following command on my computer (local host) but not from the web.

This is the key into where your issue lies. If I had to make a guess based on experience, your host has the safe_mode directive enabled. With this enabled, you can only execute files from a specific directory, namely those located in the path pointed to by the safe_mode_exec_dir directive. Check your php.ini configuration file to make sure that safe mode isn't what's causing this problem for you.

cath2010

6:05 am on Apr 9, 2010 (gmt 0)

10+ Year Member



I have already check the safe_mode directive and it is disabled. So, normally it is not the source of the problem :-(

Matthew1980

1:02 pm on Apr 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there cath2010,

Is this a linux server, if so, I dont think as it would execute a windows built application, it would need to be a linux app? Correct me if I'm wrong..

Cheers,
MRb

cath2010

8:41 pm on Apr 16, 2010 (gmt 0)

10+ Year Member



Dear Matthew,

This is maybe the problem. I asked to the person who has created the web page and he is not sure at 100% but he thinks that the server is a Linux server. If it is the case, I should built the application on Linux platform. I cannot make the test easily because I don't have Linux on my computer. Nevertheless, I will analyse this possible cause.

Thanks for your answer,

Best regards,

Catherine

Readie

10:44 pm on Apr 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Create a new .php file on the server with the following contents:

<?php
phpinfo();
?>

And open it in your browser.

First line next to System: should tell you if it's Linux or not.

cath2010

9:25 am on Apr 17, 2010 (gmt 0)

10+ Year Member



Dear Readie,

Thanks for your answer. It is a Linux server and explains thus why I was unable to run the executable file from the server.

Cheers,

Catherine

Matthew1980

6:19 pm on Apr 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Cath2010,

Ah, this explains the problem. I have tested the code you have written and under a windows server it does everything as it should do!

So at least this answers two questions for you: Firstly your code is fine; Secondly: Linux servers don't run window's built applications, pretty obvious when you think about it.

Readie: Thanks for the pointer, I had not noticed that part in the phpinfo(); before.

What Development suit are you using? Just out of curiosity, I personally only have/use Visual Basic, and use that to create 'console' type applications for use in this context.

But you now have me curios to creating a 'linux-friendly' application, as I think it would be handy to know for event like this in the future, clients with linux servers & not windows servers, this then gives a flexibility to your repertoire.

A quick search via google confirms that you can (with the help of a plugin) create linux style applications from visual studio .net

Anyway, thank you for keeping us posted of your progress, and hopefully can get this completely resolved.

Cheers,
MRb

CyBerAliEn

4:07 pm on Apr 19, 2010 (gmt 0)

10+ Year Member



If your code is 100% fully C++ that you developed yourself (or another developer)... you should be able to just upload the source code to the server and use some linux commands to compile and run the application; no? I am by no means a linux specialist, but I do recall knowing that such abilities natively exist within it

However... this would not be an option (I believe) if your "C++" was generated via a program like Visual Basic or .NET (since a huge part of the "real" code is done by that software itself).

Just some thoughts.


How complicated/lengthy is your C++ code? Given, all programming is different in terms of syntax, but C++ and PHP are not too far apart compared to some. It is not a far-stretch that one could easily (though with some time) convert some C++ classes/functions to PHP classes/functions. This would seem ideal (to me) given that you are putting this on the web and because it is connecting with mySQL (which is also probably on the same server/web?) [which is something PHP excels at/has built in]. I would take the amount of code as a consideration in this... if we're talking 3 or 4 simple functions that add things together (or something), I would just re-write them in PHP; but if we're talking multiple classes, thousands of lines of code... then I would probably but this as "step Z" and try other methods first... lol.

rocknbil

4:53 pm on Apr 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



. . . you should be able to just upload the source code to the server and use some linux commands to compile and run the application; no?


Agreed, a command line compiler will do the trick (with exceptions.) The C++ has been compiled for Windoze machine code. My Cx is little rusty here too, but

your "C++" was generated via a program like Visual Basic


Visual Studio. :-) VB is for Basic which I'm pretty sure is Windoze specific, I use it all the time but only on Windoze OS so cant' verify if it successfully ports across other OS's.

There is only one possible hang up, but don't think it will be the case looking at your original code that was printed to the screen: if the C++ compiles windows specific libraries into it, they may fail on Linux.

CyBerAliEn

5:06 pm on Apr 19, 2010 (gmt 0)

10+ Year Member



Visual Studio. :-) VB is for Basic

My bad. lol. I am an engineer by profession (industrial/systems/operations), so my knowledge of Windows programming = poor. My vast experience with development is solely in PHP, with dabblings in C++ and Java along the way. But I always hated using that Visual crap as our 'programming language tool' in various university courses --- it always felt like a "half-way" language to me, and I didn't like that [though obviously fast development = +++].


And if I am not mistaken... if you drop C source (maybe C++ too?) in your "cgi-bin", won't the server automatically compile/run the code for you? (more thoughts)

cath2010

9:29 pm on Apr 19, 2010 (gmt 0)

10+ Year Member



Dear all,

Thanks for your different answers. I will analyse your different recommendations to determine the best solution to my problem.

Please find hereafter some answers to your questions:

1. I am using Visual Studio C++

2. The idea would be to include different applications. One of the apllications is relatively simple and an option could be to transform the code to PHP code. Nevertheless, an other one uses openGL libraires,.. and it could maybe be more difficult to transform it.

I will keep you informed of my progress (maybe not before next week because at the moment I cannot devote a lot of time to this).

Cheers,

Catherine

rocknbil

6:17 pm on Apr 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if you drop C source (maybe C++ too?) in your "cgi-bin", won't the server automatically compile/run the code for you?


Don't know . . . and if I don't know, would be too afraid to try. :-P