Exec functions with php

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 1
Joined: Thu Dec 06, 2012 3:02 pm

Exec functions with php

Postby nishaun » Thu Dec 06, 2012 3:18 pm

Hello I am on a dedicated Apache server... I have been trying to run a test file exec works but I can't get any output. The function neither creates a file nor returns output... (I also tried an int main)
Here is the code

Code: Select all

echo "lalla";
echo exec("testfile.exe");
echo "lalla";
print exec("uptime");
echo "\n<br/>";
echo "<br/>";
$output = shell_exec('testfile.exe');
echo "<pre>$output</pre>x";
print  shell_exec("uptime")or die('error');
print  shell_exec("testfile.exe")or die('error');

This returns...

lallalalla 00:09:29 up 2 days, 15:08, 1 user, load average: 0.00, 0.00, 0.00
XXXXXX Jun 29 09:36 (modemcableXXXXXXX)
x 00:09:29 up 2 days, 15:08, 1 user, load average: 0.00, 0.00, 0.00

here is the code in testfile.exe (C++)

Code: Select all

#include <iostream>
#include <fstream>

using namespace std;

char main(int argc, char *argv[]){
   char sam[]="done";
   fstream finals;
   finals.open("rin3d.txt",ios::out | ios::app );


   return sam[0];


User avatar
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Exec functions with php

Postby egami » Fri Dec 14, 2012 8:53 am

Two things to remember.

1. If you're trying to run a shell argument from the webserver, the shell is being run as "apache" or "nobody" depending on your distro..

Thus, you'll need to make sure that this user has sudo privis in order to run the commands in question.

2. When you run the PHP script in your shell, it's running as you, and will have completely different permissions.

You can see all of this by doing tail -f /var/log/http/errors.log

Return to “PHP coding => General”

Who is online

Users browsing this forum: Majestic-12 [Bot] and 3 guests