note 48141 added to function.exec

From: Date: Mon, 13 Dec 2004 18:38:05 +0000
Subject: note 48141 added to function.exec
Groups: php.notes 
Request: Send a blank email to php-notes+get-81761@lists.php.net to get a copy of this message
This will execute a exectuable from the command window without the command window even being seen. Much help was given by Michael Sheakoski as he stated below this in one of his other scripts. The problem I had was that I want to have arguments be sent to the .exe. This will run the exe and then whatever it returned will be dealt with. Tested with: Windows XP Pro SP1 Apache2 PHP 4.3.x IIS6 as well Here is the snippet. <?php // Get a good ol' timestamp. Unique and it works. $unixtime = time(); // Sets up your exe or other path. $cmd = 'C:\\path\\to\\program\\argue2.exe'; // Setup an array of arguments to be sent. $arg[] = '1'; $arg[] = '2'; $arg[] = '3'; $arg[] = '4'; $arg[] = '5'; // Pick a place for the temp_file to be placed. $outputfile = 'C:\\path\\to\\tmp\\unixtime.txt'; // Setup the command to run from "run" $cmdline = "cmd /C $cmd " . implode(' ', $arg) . " > $outputfile"; // Make a new instance of the COM object $WshShell = new COM("WScript.Shell"); // Make the command window but don't show it. $oExec = $WshShell->Run($cmdline, 0, true); // Read the file file. $output = file($outputfile); // Delete the temp_file. unlink($outputfile); // Take the output and break the array apart. // If you don't know what is coming out do: // print_r("$output"); foreach($output as $temp_output) { // Check the output's return code. if($temp_output == 1) { // Tell the user it was correct. echo "All is good"; }else{ // Tell the user something goofed. echo "Didn't go smoothly."; } } ?> ---- Manual Page -- http://www.php.net/manual/en/function.exec.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+48141 Delete -- http://master.php.net/manage/user-notes.php?action=delete+48141&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48141&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48141&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48141&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48141&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48141&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+48141&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#81761) next »