Problem using PHP exec command to execute a batch file

168
June 12, 2019, at 04:40 AM

I'm attempting to get PHP to call a batch file which will take an RTF file and convert it to a PDF using an OpenOffice macro. I've tested the batch file on the command line and it works fine, but I'm not having any luck calling and using the same batch file from PHP.

My machine OS is XP professional SP 3. I'm running IIS 6 and PHP version 5.2.9. I've granted execute permissions to the internet user on c:\windows\system32\cmd.exe. I specified the full path to the batch file being executed and the full path to the RTF file to be converted.

The PHP looks like this where $arg is the RTF to be converted:

$arg = "C:\\web_root\\whatever\\tempOutput.rtf";
$command = "c:\\windows\\system32\\cmd.exe /c c:\\web_root\\whatever\\convert.bat $arg";

Then inside a try-catch I call the exec command:

exec("$command 2>&1 && exit", $ret, $err);

I echo the results after the catch:

echo "ret: ";
print_r ($ret);
print "<br>";
echo "err is ";
echo $err;
print "<br>";
echo "DONE!";

And this is what I see:

ret: Array ( ) 
err is 0
DONE!

The RTF file does not get converted and I'm not seeing the errors. Any ideas on what I can try next? Thanks!!!

Answer 1

I'm going to bet this is about permissions.

In a typical setup, PHP runs as apache - so you'll want to make sure apache has the rights to execute the batch file.

also, check this relevant SO question, and this google search.

Answer 2

Looks like the output array is empty. Is your batch script supposed to have output?

Also, escapeshellcmd and escapeshellarg should be used

Answer 3

Are you using IIS as your webserver? If so, the PHP exec function will not work by default and you should NOT circumvent the security measures that prevent it from running.

Check your event viewer and you should find some errors pertaining to your problem. Run a query through google for: IIS PHP exec. This should give you a large selection of information about the problem.

Basically, the PHP exec function tries to fork a new cmd.exe instance. IIS prohibits this because it could open a security hole in the system.

The best solution that I have come up with is to have your php script either write the command that you want to execute to a flat file or make a database entry. You will then need to write a seperate script that is launched by the windows scheduler to run every 10 minutes or so that will check your flat file or database for commands to run. The new script will then run the commands and then place either the results or an execution confirmation that your web app will be able to access at a later time.

It's a kludge for sure.

Answer 4

Is PHP running in safe-mode? If so, shell commands are escaped with escapeshellcmd. Perhaps this is the problem?

Do you have control of the server running the PHP script?

Rent Charter Buses Company
READ ALSO
Syntax error &ldquo;undefined (&rdquo;. Where is the source of the error?

Syntax error “undefined (”. Where is the source of the error?

So I have what I believe is a syntax errorWhen I analyse the code in the codepen editor i get an error message of "undefined ("

114
Is hoisting only implemented in JavaScript?

Is hoisting only implemented in JavaScript?

I know that c/c++/java has no hoistingBut JavaScript has variable hoisting and function hoisting

109
Dynamically switch flex size in Vuejs?

Dynamically switch flex size in Vuejs?

i am trying to switch the flex size using VuejsI have three radio buttons and a select

147