I have included the code that I am trying to debug below. Basically, what I am doing is running ghostscript to print a PDF to my printer (which is the text in the windExec. Now, when I run the same line of text in the cmd prompt, it works fine and prints. When I run this through php, it fails.
Now, the function windExec, I got from php.net under the exec help section. It basically generates a COM object that will simulate a run dialog box. So I can execute any command line program, such as ghostscript.
Does anyone have any ideas how to get this to work? I know the function works for simple things like a ping or ipconfig, but not for this particular command.
Similarly, does anyone know how to remotely execute programs/command line programs using PHP so I can get this remote printing capability on a windows box?
<?php
define ('EXEC_TMP_DIR', 'C:\Temp');
echo windExec('"C:\Program Files\GSView\gsview\gsprint.exe" "C:\invoice.pdf"', '');
function windExec($cmd,$mode=''){
// runs a command line and returns
// the output even for Wind XP SP2
// example: $cmd = "fullpath.exe -arg1 -arg2"
// $outputString = windExec($cmd, "FG");
// OR windExec($cmd);
// (no output since it runs in BG by default)
// for output requires that EXEC_TMP_DIR be defined
// Setup the command to run from "run"
$cmdline = 'cmd /C '.$cmd;
//$cmdline=$cmd;
echo $cmdline;
$outputfile="";
// set-up the output and mode
if ($mode=='FG'){
$outputfile = EXEC_TMP_DIR . "\" . time() . ".txt";
$cmdline .= " > $outputfile";
$m = true;
}
else $m = false;
// Make a new instance of the COM object
$WshShell = new COM("WScript.Shell");
// Make the command window but dont show it.
$oExec = $WshShell->Run($cmdline, 0, $m);
if ($outputfile){
// Read the tmp file.
$retStr = file_get_contents($outputfile);
// Delete the temp_file.
unlink($outputfile);
}
else $retStr = "";
return $retStr;
}